第十八天| 513.找树左下角的值
513_关键字:二叉树,递归,回溯
513_题目链接
代码实现
package LeetCode;
public class YangSibo_513 {
}
class YangSibo_513_1{
int maxdepth = Integer.MIN_VALUE;
int result = 0;
public int findBottomLeftValue(TreeNode root) {
if(root.left == null && root.right == null) return root.val;
getDepleft(0,root);
return result;
}
public void getDepleft (int depth,TreeNode cur) {
if(cur.left == null && cur.right == null) {
if(depth > maxdepth) {
maxdepth = depth;
result = cur.val;
}
return;
}
if(cur.left != null) {
depth ++;
getDepleft(depth,cur.left);
depth --;
}
if(cur.right != null) {
depth ++;
getDepleft(depth,cur.right);
depth --;
}
return;
}
}
解题注意事项
1、递归三要素,回溯的路径要完整
2、合理的运用全局变量