leetcode刷题|构造二叉树,来挑战吧?

513.找左树下角的值

力扣链接

题解

解题思路

  • 使用层序遍历-迭代法

  • 定义一个队列,把节点值offer到队列,获取队列的长度,根据长度进行循环

  • i==0的为左子树左孩子节点

Java代码

/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode() {}
 *     TreeNode(int val) { this.val = val; }
 *     TreeNode(int val, TreeNode left, TreeNode right) {
 *         this.val = val;
 *         this.left = left;
 *         this.right = right;
 *     }
 * }
 */
class Solution {
    public int findBottomLeftValue(TreeNode root) {
        //迭代法(层序遍历)
        Deque<TreeNode> que = new LinkedList<>();
        que.offer(root);
        int res = 0;
        
        while(!que.isEmpty()) {
            int size = que.size();
            for (int i=0; i < size; i++) {
                TreeNode node = que.poll();
                //左树左孩子值
                if (i == 0) {
                    res = node.val;
                }
                if (node.left != null) {
                    que.offer(node.left);
                }
                if (node.right != null) {
                    que.offer(node.right);
                }
            }
        }
        return res;
    }
}

112.路径之和

力扣链接

题解

解题思路

  • 采用递归遍历

  • 确定返回值和参数:因为只要找到符合条件的路径即返回,所以无需返回值;参数是TreeNode

  • 确定终止条件:为叶子节点,且目标值减去叶子节点值为0,返回true

  • 确定单层逻辑:左右节点不为空,则递归遍历,如果返回的为true则直接返回

Java代码

/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode() {}
 *     TreeNode(int val) { this.val = val; }
 *     TreeNode(int val, TreeNode left, TreeNode right) {
 *         this.val = val;
 *         this.left = left;
 *         this.right = right;
 *     }
 * }
 */
class Solution {
    //根据前序遍历 因为不需要处理递归函数返回值 所以不要返回值
    public boolean hasPathSum(TreeNode root, int targetSum) {
        if (root == null) {
            return false;
        }
        //中
        targetSum -= root.val;
        //终止条件
        if (root.left == null && root.right == null) {
            return targetSum == 0 ? true : false;
        }
        //左
        if (root.left != null) {
            boolean left = hasPathSum(root.left, targetSum);
            if (left) {
                return true; //已经找到
            }
        }
        //右
        if (root.right != null) {
            boolean right = hasPathSum(root.right, targetSum);
            if (right) {
                return true; // 已经找到
            }
        }
        return false;
    }
}

113.路径之和II

力扣链接

题解

解题思路

  • 与112不同之处,该题需要返回值,采用递归遍历

  • 确定返回值和参数:返回值为二维数组,记录所有符合条件的路径;参数是TreeNode,count

  • 确定终止条件:节点为叶子节点,且count==0,返回值中添加该路径

  • 确定单层逻辑:左/右树不为空,则递归遍历,最重要的是:每次回溯时,要把添加在path数组中的最后一个值移除掉(递归中要先把节点值offer到path数组中,count是目标值-=)

Java代码

/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode() {}
 *     TreeNode(int val) { this.val = val; }
 *     TreeNode(int val, TreeNode left, TreeNode right) {
 *         this.val = val;
 *         this.left = left;
 *         this.right = right;
 *     }
 * }
 */
class Solution {
    //定义全局变量 路径和返回值
    List<Integer>path = new LinkedList<>();
    List<List<Integer>> res = new ArrayList<List<Integer>>();
    public List<List<Integer>> pathSum(TreeNode root, int targetSum) {
        travesal(root, targetSum);
        return res;
    }
    //遍历节点
    public void travesal(TreeNode node, int targetSum) {
        if (node == null) {
            return;
        }
        path.add(node.val);
        targetSum -= node.val;
        //终止条件
        if (node.left == null && node.right == null && targetSum == 0) {
            res.add(new ArrayList<>(path));
        }
        //左
        travesal(node.left, targetSum);
        //右
        travesal(node.right, targetSum);
        //回溯
        path.remove(path.size() - 1);
    }
}

116.从中序和后序构造二叉树

力扣链接

题解

解题思路:

  • 通过递归的方法

  • 确定返回值和参数:返回是二叉树TreeNode,参数是中序数组,数组开始下标,数组结束下标,后序数组,数组开始下标,数组结束后下标

  • 确定终止条件:中序数组(或后序数组)的开始下标>=结束下标

  • 确定单层逻辑

    • 根节点是后序数组最后一个元素rootValue=postorder[postorder.length-1]

    • 在中序数组中找到根节点的位置rootIndex,作为切割点。rootIndex=inorder[rootValue]

    • 保存中序数组中,左子树的个数 onLeft = rootIndex-inBegin

    • 先切割中序数组,获取其左子树和右子树

    • 再切割后序数组,获取其左子树和右子树

    • 递归遍历中序数组区间,后序数组区间

Java代码

/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode() {}
 *     TreeNode(int val) { this.val = val; }
 *     TreeNode(int val, TreeNode left, TreeNode right) {
 *         this.val = val;
 *         this.left = left;
 *         this.right = right;
 *     }
 * }
 */
 --从中序和后序构造二叉树
class Solution {
    HashMap<Integer, Integer> map = new HashMap<>();
    public TreeNode buildTree(int[] inorder, int[] postorder) {
        //保存中序的下标
        for (int i = 0; i < inorder.length; i++) {
            map.put(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 = map.get(postorder[postEnd-1]);
        //构造节点
        TreeNode root = new TreeNode(inorder[rootIndex]);
        //保存二叉树 中序中左子树的个数
        int onLeft = rootIndex - inBegin;
        //左子树,先分割中序 左子树[inBegin, rootIndex) 中序右子树[rootIndex + 1, intEnd) 左闭右开
        //根据中序左子树的个数,分割后序数组 左子树 [postBegin, postBegin+onLeft) 右子树 [postBegin+onLeft, postEnd - 1)左闭右开
        root.left = findNode(inorder, inBegin, rootIndex, postorder, postBegin, postBegin + onLeft);
        root.right = findNode(inorder, rootIndex + 1, inEnd, postorder, postBegin + onLeft, postEnd - 1);
        return root;
    }
}

105.从前序和中序构造二叉树

力扣链接

题解

解题思路:与中序和后序思路一样,只是根节点是前序的首个元素

Java代码

/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode() {}
 *     TreeNode(int val) { this.val = val; }
 *     TreeNode(int val, TreeNode left, TreeNode right) {
 *         this.val = val;
 *         this.left = left;
 *         this.right = right;
 *     }
 * }
 */
class Solution {
    HashMap<Integer, Integer> map = new HashMap<>();
    public TreeNode buildTree(int[] preorder, int[] inorder) {
        for (int i =0; i<inorder.length;i++) {
            map.put(inorder[i], i);
        }

        return findNode(preorder, 0, preorder.length, inorder, 0, inorder.length);
    }

    public TreeNode findNode(int[] preorder, int preBegin, int preEnd, int[] inorder, int inBegin, int inEnd) {
        //终止条件
        if (preBegin >= preEnd || inBegin >= inEnd) {
            return null;
        }
        //获取根节点在中序中的位置(根节点是前序遍历的第一个元素)
        int rootIndex = map.get(preorder[preBegin]);
        //构造节点
        TreeNode root = new TreeNode(inorder[rootIndex]);
        //中序遍历中左子树的个数
        int onLeft = rootIndex - inBegin;
        //根据中序左子树的个数,分割前序数组 左子树 [preBegin + 1, preBegin+onLeft+1) 右子树 [preBegin+onLeft+1, preEnd)左闭右开
        //左子树,先分割中序 左子树[inBegin, rootIndex) 中序右子树[rootIndex + 1, intEnd) 左闭右开
        root.left = findNode(preorder, preBegin+1, preBegin + onLeft + 1, inorder, inBegin, rootIndex);
        root.right = findNode(preorder, preBegin + onLeft + 1, preEnd, inorder, rootIndex + 1, inEnd);
        return root;
    }
}

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值