刷题12:热题100

class Solution {
    public void flatten(TreeNode root) {
        Stack<TreeNode> st = new Stack<>();
        if (root==null) return;
        st.push(root);
        TreeNode pre = null;
        while(!st.isEmpty()){
            TreeNode temp = st.pop();
            if (pre!=null){
                pre.right = temp;
                pre.left = null;
            }
            if (temp.right!=null){
                st.push(temp.right);
            }
            if (temp.left!=null) {
                st.push(temp.left);
            }
            pre = temp;
        }
    }
}
class Solution {
    TreeNode pre = null;
    public void flatten(TreeNode root) {
        if (root==null) return; 
        flatten(root.right);
        flatten(root.left);
        root.right = pre;
        root.left = null;
        pre = root;
    }
}

class Solution {
    public TreeNode buildTree(int[] preorder, int[] inorder) {
        return findNode(preorder,0,preorder.length,inorder,0,inorder.length);
    }
    public TreeNode findNode(int[] preorder, int prestart, int preend, int[] inorder, int instart, int inend){
        if (prestart==preend) return null;
        if (instart==inend) return null;
        TreeNode root = new TreeNode(preorder[prestart]);
        int inindex = 0; // 中序遍历的根节点
        for (inindex=0;inindex<inend;inindex++){
            if (inorder[inindex]==root.val){
                break;
            }
        }
        int leftlen = inindex - instart;
        // 注意边界条件【先闭后开】
        TreeNode left = findNode(preorder,prestart+1,prestart+1+leftlen,inorder,instart,inindex);
        TreeNode right = findNode(preorder,prestart+1+leftlen,preend,inorder,inindex+1,inend);
        root.left = left;
        root.right = right;
        return root;
    }
}

class Solution {
    public int pathSum(TreeNode root, long targetSum) {
        if (root == null) {
            return 0;
        }

        int ret = rootSum(root, targetSum);
        ret += pathSum(root.left, targetSum);
        ret += pathSum(root.right, targetSum);
        return ret;
    }

    public int rootSum(TreeNode root, long targetSum) {
        int ret = 0;

        if (root == null) {
            return 0;
        }
        int val = root.val;
        if (val == targetSum) {
            ret++;
        } 

        ret += rootSum(root.left, targetSum - val);
        ret += rootSum(root.right, targetSum - val);
        return ret;
    }
}

class Solution {
    public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) {
        if (root==null || root==p || root==q) return root;
        TreeNode left = lowestCommonAncestor(root.left,p,q);
        TreeNode right = lowestCommonAncestor(root.right,p,q);
        if (left==null && right!=null){
            return right;
        }else if (left!=null && right==null){
            return left;
        }else if (left==null && right==null){
            // 说明左右子树都不包含p,q
            return null;
        }
        return root;
    }
}

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值