📌 本文系统梳理二叉树属性在lecode上的一些热门题目附完整 Java 代码 + 关键逻辑解析+lecode练习地址。适合 LeetCode 刷题 &
面试复习!

6.257二叉树的所有路径

lecode题目:257. 二叉树的所有路径 - 力扣(LeetCode)

思路

  • 使用先序遍历的方式实现节点的访问
  • 使用SpringBuilder对找的路径进行拼接

实施过程

1.确定递归返回值和参数,参数为节点和需要进行拼接的节点的字符串

 public void deal(TreeNode root,String s) 

2.终止条件,当前节点为空即末尾

if (root == null) return;

3.单层递归逻辑,左右子数为空时,则该节点为叶子,即该路径为根到该叶子的最大路径,并且存入到数组中

        if (root.left == null && root.right == null) {
            result.add(new StringBuilder(s).append(root.val).toString());
        }
        //在原来的字符串基础上拼接
        String temp=new StringBuilder(s).append(root.val).append("->").toString();
        deal(root.left,temp);
        deal(root.right,temp);

4.最终代码

class Solution_257 {
    List<String> result = new ArrayList<>();//全局变量
    public List<String> binaryTreePaths(TreeNode root) {
        deal(root,"");
        return result;
    }
    public void deal(TreeNode root,String s) {
        if (root == null) return;
        if (root.left == null && root.right == null) {
            result.add(new StringBuilder(s).append(root.val).toString());
        }
        //在原来的字符串基础上拼接
        String temp=new StringBuilder(s).append(root.val).append("->").toString();
        deal(root.left,temp);
        deal(root.right,temp);
    }
}

7.404左叶子之和

lecode题目:

思路

  • 找到所有子数的左节点
  • 存储所有节点左右子树的左节点,就需要定义两个变量存储节点个数

实施过程

1.确定递归返回值和参数,返回的值为整型,参数为当前根节点

 public int sumOfLeftLeaves(TreeNode root) 

2.终止条件,当前节点为空即末尾

        if (root == null) return 0;

3.单层递归逻辑,定义两个变量用于接收左右子数的左节点之和,还要判断该次递归该节点是否为左节点,存入到minvalue变量用于返回

		int leftvalue=sumOfLeftLeaves(root.left);//左子树的左节点之和
        int rightvalue=sumOfLeftLeaves(root.right);//右子树的左节点之和
        int minvalue=0;
        //判断左节点
        if(root.left!=null &&root.left.left==null &&root.left.right==null){
            minvalue=root.left.val;
        }
        int sum=leftvalue+rightvalue+minvalue;
        return sum;

4.最终代码

class Solution_404 {
    public int sumOfLeftLeaves(TreeNode root) {
        if (root == null) return 0;
        int leftvalue=sumOfLeftLeaves(root.left);//左子树的左节点之和
        int rightvalue=sumOfLeftLeaves(root.right);//右子树的左节点之和
        int minvalue=0;
        //判断左节点
        if(root.left!=null &&root.left.left==null &&root.left.right==null){
            minvalue=root.left.val;
        }
        int sum=leftvalue+rightvalue+minvalue;
        return sum;
    }
}

8.513找树左下角的值

lecode题目:513. 找树左下角的值 - 力扣(LeetCode)

思路

  • 定义全局变量存储最大深度
  • 当到达叶子节点时判断是否为最深,是就找到最下的值

实施过程

1.确定递归返回值和参数,参数为节点和层数

 private void findleftvalue(TreeNode root,int depth)

2.终止条件,当前节点为空即末尾

if(root==null)return;

3.单层递归逻辑,判断是否为最深,递归左右子数

 if(root.left==null && root.right==null){
            if(depth>Deep){
                value=root.val;
                Deep=depth;
            }
        }
        if(root.left!=null) findleftvalue(root.left,depth+1);
        if(root.right!=null) findleftvalue(root.right,depth+1);

4.最终代码

class Solution_513 {
    private int Deep=-1;
    private int value=0;
    public int findBottomLeftValue(TreeNode root) {
        value=root.val;
        findleftvalue(root,0);
        return value;

    }
    private void findleftvalue(TreeNode root,int depth) {
        if(root==null)return;
        if(root.left==null && root.right==null){
            if(depth>Deep){
                value=root.val;
                Deep=depth;
            }
        }
        if(root.left!=null) findleftvalue(root.left,depth+1);
        if(root.right!=null) findleftvalue(root.right,depth+1);
    }
}

9.路径总和

lecode题目:112. 路径总和 - 力扣(LeetCode)

思路

  • 每遍历一个节点递归的传入的参数是目标值-该节点的值,作为下一次递归的值

实施过程

1.确定递归返回值和参数,返回的值为布尔型,参数为节点和改变的目标值

public boolean hasPathSum(TreeNode root, int targetSum)

2.终止条件,当前节点为空即末尾

if (root == null) return false;

3.单层递归逻辑,如果到达叶子节点,检查剩余 targetSum 是否等于该节点值,如果不是叶子,继续向左右子树递归(传入新的 targetSum = 原值 - 当前值),只要任一子树返回 true,最终结果就是 true

		if(root.left == null && root.right == null) return targetSum == root.val;
        return hasPathSum(root.left, targetSum - root.val) || hasPathSum(root.right, targetSum - root.val);

4.最终代码

class Solution_112 {
    public boolean hasPathSum(TreeNode root, int targetSum) {
        if (root == null) return false;
        if(root.left == null && root.right == null) return targetSum == root.val;
        return hasPathSum(root.left, targetSum - root.val) || hasPathSum(root.right, targetSum - root.val);
    }
}
Logo

汇聚全球AI编程工具,助力开发者即刻编程。

更多推荐