給定一個二叉樹,在樹的最后一行找到最左邊的值。
代碼
/*** Definition for a binary tree node.* public class TreeNode {* int val;* TreeNode left;* TreeNode right;* TreeNode(int x) { val = x; }* }*/
class Solution {int maxL=0,maxN=0;public int findBottomLeftValue(TreeNode root) {BottomLeft(root,1);return maxN;}public void BottomLeft(TreeNode root,int level) {//先序遍歷if(root==null) return;if(maxL<level)//如果找出更深的節點,替換掉{maxL=level;maxN=root.val;}BottomLeft(root.left,level+1);BottomLeft(root.right,level+1);}
}