思考:
本題比較簡單,是運用廣度優先算法,通過隊列存儲同一深度元素。
public int findBottleLeftValue(TreeNode root){ Queue<TreeNode> q = new LinkedList<TreeNode>(); q.offer(root); int result = 0; while (!q.isEmpty()) { int size = q.size(); result = q.peek().val; while (size>0) { TreeNode tempNode = q.poll(); size--; if(tempNode.left!=null){ q.offer(tempNode.left); } if(tempNode.right!=null){ q.offer(tempNode.right); } } } return result; }
新聞熱點
疑難解答