C#代码随想录算法训练营day18|二叉树找树左下角值、路径总和、从中序后序遍历构造二叉树

LeetCode513 找树左下角的值

题目:

给定一个二叉树的 根节点 root,请找出该二叉树的 最底层 最左边 节点的值。
假设二叉树中至少有一个节点。

示例 1:
在这里插入图片描述

输入: root = [2,1,3]
输出: 1

示例 2:在这里插入图片描述

输入: [1,2,3,4,null,5,6,null,null,7]
输出: 7

代码:
/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     public int val;
 *     public TreeNode left;
 *     public TreeNode right;
 *     public TreeNode(int val=0, TreeNode left=null, TreeNode right=null) {
 *         this.val = val;
 *         this.left = left;
 *         this.right = right;
 *     }
 * }
 */
 //迭代BFS(层序遍历)
public class Solution {
    public int FindBottomLeftValue(TreeNode root) {
        Queue<TreeNode> queue = new Queue<TreeNode>();
        if (root == null) return -1;
        queue.Enqueue(root);
        int res = 0;
        while (queue.Count > 0)
        {
            int size = queue.Count;
            for (int i = 0; i < size; i++)
            {
                TreeNode cur = queue.Dequeue();
                if (i == 0)
                {
                    res = cur.val;
                }
                if (cur.left != null)
                {
                    queue.Enqueue(cur.left);
                }
                if (cur.right != null)
                {
                    queue.Enqueue(cur.right);
                }
            }
        }
        return res;
    }
}
//递归+回溯
public class Solution {
    int maxDepth = int.MinValue;
    int res;
    public int FindBottomLeftValue(TreeNode root) {
        DFS(root, 0);
        return res;
    }

    public void DFS(TreeNode root, int depth)
    {
        if (root.left == null && root.right == null)
        {
            if (depth > maxDepth)
            {
                maxDepth = depth;
                res = root.val;
            }
            return;
        }
        if (root.left != null)
        {
            depth++;
            DFS(root.left, depth);
            depth--;//回溯
        }
        if (root.right != null)
        {
            depth++;
            DFS(root.right, depth);
            depth--;//回溯
        }
        return;
    }
}

LeetCode 112 路径总和

题目:

给你二叉树的根节点 root 和一个表示目标和的整数 targetSum 。判断该树中是否存在 根节点到叶子节点 的路径,这条路径上所有节点值相加等于目标和 targetSum 。如果存在,返回 true ;否则,返回 false 。
叶子节点 是指没有子节点的节点。

示例 1:
在这里插入图片描述

输入:root = [5,4,8,11,null,13,4,7,2,null,null,null,1], targetSum = 22
输出:true
解释:等于目标和的根节点到叶节点路径如上图所示。

示例 2:
在这里插入图片描述

输入:root = [1,2,3], targetSum = 5
输出:false
解释:树中存在两条根节点到叶子节点的路径:
(1 --> 2): 和为 3
(1 --> 3): 和为 4
不存在 sum = 5 的根节点到叶子节点的路径。

示例 3:
输入:root = [], targetSum = 0
输出:false
解释:由于树是空的,所以不存在根节点到叶子节点的路径。
在这里插入图片描述

代码:
//DFS
public class Solution {
    public bool HasPathSum(TreeNode root, int targetSum) 
    {
        if (root == null) return false;
        targetSum -= root.val;
        if (root.left == null && root.right == null)
        {
            return targetSum == 0;
        }
        if (root.left != null)
        {
            bool left = HasPathSum(root.left, targetSum);
            if (left) return true;
        }
        if (root.right != null)
        {
            bool right = HasPathSum(root.right, targetSum);
            if (right) return true;
        }
        return false;
    }
}
//BFS
public class Solution {
    public bool HasPathSum(TreeNode root, int targetSum) 
    {
        Queue<TreeNode> queue = new Queue<TreeNode>();
        Queue<int> queue1 = new Queue<int>();
        if (root == null) return false;
        queue.Enqueue(root);
        queue1.Enqueue(root.val);
        while (queue.Count > 0)
        {
            int size = queue.Count;
            for (int i = 0; i < size; i++)
            {
                TreeNode cur = queue.Dequeue();
                int sum = queue1.Dequeue();
                if (cur.left == null && cur.right == null && sum == targetSum)
                {
                    return true;
                }
                if (cur.left != null)
                {
                    queue.Enqueue(cur.left);
                    queue1.Enqueue(sum + cur.left.val);
                }
                if (cur.right != null)
                {
                    queue.Enqueue(cur.right);
                    queue1.Enqueue(sum + cur.right.val);
                }
            }
        }
        return false;
    }
}

LeetCode113 路径总和 II

题目:

给你二叉树的根节点 root 和一个整数目标和 targetSum ,找出所有 从根节点到叶子节点 路径总和等于给定目标和的路径。
叶子节点 是指没有子节点的节点。

示例 1:
在这里插入图片描述

输入:root = [5,4,8,11,null,13,4,7,2,null,null,5,1], targetSum = 22
输出:[[5,4,11,2],[5,8,4,5]]

示例 2:
在这里插入图片描述

输入:root = [1,2,3], targetSum = 5
输出:[]

示例 3:
输入:root = [1,2], targetSum = 0
输出:[]

代码:
//DFS
public class Solution {
    public IList<IList<int>> PathSum(TreeNode root, int targetSum) {
        IList<IList<int>> res = new List<IList<int>>();
        IList<int> list = new List<int>();
        if (root == null) return res;
        PreOrderDFS(root, targetSum, res, list);
        return res;
    }

    public void PreOrderDFS(TreeNode root, int targetSum, IList<IList<int>> res, IList<int> list)
    {
        list.Add(root.val);
        targetSum -= root.val;
        //遇到叶子节点
        if (root.left == null && root.right == null && targetSum == 0)
        {
            //找到和为targetSum的路径
            res.Add(new List<int>(list));
        }
        if (root.left != null)
        {
            PreOrderDFS(root.left, targetSum, res, list);
            list.RemoveAt(list.Count - 1);//回溯
        }
        if (root.right != null)
        {
            PreOrderDFS(root.right, targetSum, res, list);
            list.RemoveAt(list.Count - 1);//回溯
        }
    }
}

LeetCode106 从中序与后序遍历序列构造二叉树

题目:

给定两个整数数组 inorder 和 postorder ,其中 inorder 是二叉树的中序遍历, postorder 是同一棵树的后序遍历,请你构造并返回这颗 二叉树 。

示例 1:
在这里插入图片描述

输入:inorder = [9,3,15,20,7], postorder = [9,15,7,20,3]
输出:[3,9,20,null,null,15,7]

示例 2:
输入:inorder = [-1], postorder = [-1]
输出:[-1]

思路:

以后序数组的最后一个元素为切割点,先切中序数组,根据中序数组,反过来再切后序数组。一层一层切下去,每次后序数组最后一个元素就是节点元素。
在这里插入图片描述
说到一层一层切割,就应该想到了递归。
来看一下一共分几步:
第一步:如果数组大小为零的话,说明是空节点了。
第二步:如果不为空,那么取后序数组最后一个元素作为节点元素。
第三步:找到后序数组最后一个元素在中序数组的位置,作为切割点
第四步:切割中序数组,切成中序左数组和中序右数组 (顺序别搞反了,一定是先切中序数组)
第五步:切割后序数组,切成后序左数组和后序右数组
第六步:递归处理左区间和右区间

代码:
//递归DFS
public class Solution {
    Dictionary<int, int> dic = new Dictionary<int, int>();
    public TreeNode BuildTree(int[] inorder, int[] postorder) {
        TreeNode root = new TreeNode();
        for (int i = 0; i < inorder.Length; i++)
        {
            dic.Add(inorder[i], i);
        }
        return FindNode(inorder, 0, inorder.Length, postorder, 0, postorder.Length);//左闭右开
    }

    public TreeNode FindNode(int[] inorder, int inBegin, int inEnd, int[] postorder, int postBegin, int postEnd)
    {
        //参数范围都是左闭右开
        if (inBegin >= inEnd || postBegin >= postEnd)//不满足左闭右开,说明没有元素,返回空树
        {
            return null;
        }
        int rootIndex = dic[postorder[postEnd - 1]];//找到后序遍历的最后一个元素在中序遍历中的位置
        TreeNode root = new TreeNode(inorder[rootIndex]);//构造节点
        int lenOfLeft = rootIndex - inBegin;//保存中序左子树个数,用来确定后续数列的个数
        root.left = FindNode(inorder, inBegin, rootIndex, postorder, postBegin, postBegin + lenOfLeft);
        root.right = FindNode(inorder, rootIndex + 1, inEnd, postorder, postBegin + lenOfLeft, postEnd - 1);
        return root;
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值