给定一个二叉树,在树的最后一行找到最左边的值。
代码
/*** 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);}
}