代码随想录训练营第十八天| 513. 找树左下角的值 112. 路径总和 106. 从中序与后序遍历序列构造二叉树

513. 找树左下角的值

513. 找树左下角的值

  1. 方法一:层序遍历(迭代法)
/**
class Solution {
    public int findBottomLeftValue(TreeNode root) {
        //本题使用迭代法(层序遍历)即可较为简单的得到答案
        Queue<TreeNode> que = new LinkedList<>();
        int res = 0 ;
        //题目中保证了二叉树至少有一个节点
        que.offer(root);
        while(!que.isEmpty()){
            int len = que.size();
            for(int i= 0 ;i < len ; i++){
                TreeNode temp = que.poll();
                if(i == 0){
                    res = temp.val;
                }
                if(temp.left != null){
                    que.offer(temp.left);
                }
                if(temp.right != null){
                    que.offer(temp.right);
                }
            }
        }
        return res;
    }
}

2. 方法二:递归法:

class Solution {
    private int DEEP = -1;
    private int value = 0 ;
    //private int deep ;
    public int findBottomLeftValue(TreeNode root) {
        //使用递归法
        value = root.val;
        findleftvalue(root,0);
        return value;
    }
    private void findleftvalue(TreeNode root, int deep){
        if(root == null){
            return ;
        }
        //终止条件
        if(root.left == null && root.right == null){
            if(deep > DEEP){
                value = root.val;
                DEEP = deep;
            }
        }
        if(root.left != null){
            findleftvalue(root.left, deep + 1);
        }
        if(root.right != null){
            findleftvalue(root.right,deep + 1);
        }
    }
}

112. 路径总和

112. 路径总和

/**
 * 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;
        }
        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;
    }
}

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

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

/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode() : val(0), left(nullptr), right(nullptr) {}
 *     TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
 *     TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
 * };
 */
class Solution {
public:
    TreeNode* buildTree(vector<int>& inorder, vector<int>& postorder) {
        return create(inorder,0,inorder.size()-1,postorder,0,postorder.size()-1);
    }
    TreeNode* create(vector<int>& inorder,int instart,int inend, vector<int>& postorder,int poststart, int postend){
        if(instart>inend){
            return nullptr;
        }
        //首先找到再中序遍历中的根节点的序号
        int rootval = postorder[postend];
        int index = 0;
        for( int i = instart ;i <= inend ; i++){
            if(rootval==inorder[i]){
                index = i;
                break;
            }
        }
        //现在得到了中序遍历中的根节点和序号
        int leftsize = index - instart ;
        TreeNode* root = new TreeNode(rootval);
        root->left = create(inorder,instart,index-1,postorder,poststart,poststart+leftsize-1);
        root->right = create(inorder,index+1,inend,postorder,poststart+leftsize,postend-1);
        return root;
    }
};
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值