二叉树从根节点出发的所有路径

二叉树从根节点出发的所有路径

在这里插入图片描述
看上图中 二叉树结构
从根节点出发的所有路径 如下
6->4->2->1
6->4->2->3
6->4->5
6->8->7
6->8->9

逻辑思路:
按照先序遍历 加 回溯法 实现

代码如下

        // 调用此方法,将根节点传递进来
        public static IList<IList<int>> Path(BinNode<int> root)
        {
            // 存储所有路径,每一项为一个路径
            List<IList<int>> resultList = new List<IList<int>>();

            List<int> list = new List<int>();
            Path(root, resultList, list);

            // 遍历打印所有路径
            foreach(var listTemp in resultList)
            {
                string msg = "";
                foreach(var item in listTemp)
                {
                    msg += "->" + item;
                }
                Console.WriteLine(msg);
            }

            return resultList;
        }

        public static void Path(BinNode<int> root, IList<IList<int>> resultList, List<int> list)
        {
            if (null == root)
            {
                return;
            }

            list.Add(root.Element);
            // 如果 左子树和右子树 都不存在,则路径结束
            if (null == root.LeftChild && null == root.RightChild)
            {
                List<int> newList = new List<int>(list);
                // 将路径存储
                resultList.Add(newList);
                return;
            }

            if (root.LeftChild != null)
            {
                Path(root.LeftChild, resultList, list);
                // 回溯
                list.RemoveAt(list.Count - 1);
            }

            if (root.RightChild != null)
            {
                Path(root.RightChild, resultList, list);
                // 回溯
                list.RemoveAt(list.Count - 1);
            }
  • 3
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
这是一个比较经典的二叉树路径问题,常用的解法是递归。对于每个节点,我们可以计算从该节点出发的最大路径和,然后将其作为递归返回值,同时更新一个局变量 maxSum,用于记录最大路径和。具体而言,设递归函数 maxPathSum(node) 表示从节点 node 出发的最大路径和,我们可以分为以下三种情况进行讨论: 1. node 为叶子节点:此时最大路径和即为该节点的值。 2. node 有左右子树:此时最大路径和可能有以下几种情况: (1) 从 node.left 出发的最大路径和加上 node.val 得到的路径和最大; (2) 从 node.right 出发的最大路径和加上 node.val 得到的路径和最大; (3) 从 node.left 出发的最大路径和加上从 node.right 出发的最大路径和再加上 node.val 得到的路径和最大。 3. node 只有左子树或右子树:此时最大路径和可能有以下两种情况: (1) 从 node.left 出发的最大路径和加上 node.val 得到的路径和最大; (2) 从 node.right 出发的最大路径和加上 node.val 得到的路径和最大。 最后,我们对根节点调用 maxPathSum(root) 即可得到最大路径和。 具体实现可以参考以下代码: ```python class Solution: def maxPathSum(self, root: TreeNode) -> int: self.maxSum = float('-inf') # 初始化局变量 maxSum self.maxPathSumHelper(root) return self.maxSum def maxPathSumHelper(self, node: TreeNode) -> int: if not node: # 递归终止条件 return 0 leftSum = max(0, self.maxPathSumHelper(node.left)) # 左子树路径和 rightSum = max(0, self.maxPathSumHelper(node.right)) # 右子树路径和 self.maxSum = max(self.maxSum, leftSum + rightSum + node.val) # 更新局变量 maxSum return max(leftSum, rightSum) + node.val # 返回从 node 出发的最大路径和 ``` 对于Java版本,相应地可以做出如下修改: ```java class Solution { private int maxSum = Integer.MIN_VALUE; // 初始化局变量 maxSum public int maxPathSum(TreeNode root) { maxPathSumHelper(root); return maxSum; } public int maxPathSumHelper(TreeNode node) { if (node == null) { // 递归终止条件 return 0; } int leftSum = Math.max(0, maxPathSumHelper(node.left)); // 左子树路径和 int rightSum = Math.max(0, maxPathSumHelper(node.right)); // 右子树路径和 maxSum = Math.max(maxSum, leftSum + rightSum + node.val); // 更新局变量 maxSum return Math.max(leftSum, rightSum) + node.val; // 返回从 node 出发的最大路径和 } } ``` 希望能够帮到你!
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值