LeetCode喜新厌旧专项-面试专项

面试手撕过的题目,顺便记录一下需要加强的题目

  • 重建二叉树
  • 二叉树的最近公共祖先
  • 二叉树的最大路径和

236 二叉树的最近公共祖先

    public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) {
        //子树问题,也可以看做先根遍历解决的
        if (root==null || p==root || q==root) return root;
        TreeNode left=lowestCommonAncestor(root.left,p,q);
        TreeNode right=lowestCommonAncestor(root.right,p,q);
        if(left==null) return right;
        if (right==null) return left;
        return root;

103 二叉树的锯齿形层序遍历

    public List<List<Integer>> zigzagLevelOrder(TreeNode root) {
        List<List<Integer>> ans=new LinkedList<>();
        if (root==null) return ans;
        Queue<TreeNode> pq=new LinkedList<>();
        pq.add(root);
        int level=1;
        while (!pq.isEmpty()){
            int sz=pq.size();
            List<Integer> temp=new LinkedList<>();
            for(int i=0;i<sz;i++){
                TreeNode node=pq.poll();
                if(level%2==0){
                    temp.add(0,node.val);
                }else{
                    temp.add(node.val);
                }
                if(node.left!=null) pq.add(node.left);
                if (node.right!=null) pq.add(node.right);
            }
            level++;
            ans.add(temp);
        }
        return ans;
    }

199 二叉树的右视图

    public List<Integer> rightSideView(TreeNode root) {
        List<Integer> ans=new LinkedList<>();
        if (root==null) return ans;
        Queue<TreeNode> pq=new LinkedList<>();
        pq.add(root);
        while (!pq.isEmpty()){
            int sz=pq.size();
            for (int i=0;i<sz;i++){
                TreeNode node=pq.poll();
                if(i==(sz-1)){
                    ans.add(node.val);
                }
                if (node.left!=null) pq.add(node.left);
                if(node.right!=null) pq.add(node.right);
            }
        }
        return ans;
    }

129 求根节点到叶节点数字之和

    int ans;
    StringBuilder sb=new StringBuilder();
    public int sumNumbers(TreeNode root) {
        dfs(root);
        return ans;
    }
    void dfs(TreeNode root){
        if (root==null) return;
        int t=sb.length();
        sb.append(root.val);
        if(root.left==null && root.right==null) ans+=Integer.parseInt(sb.toString());
        dfs(root.left);
        dfs(root.right);
        sb.delete(t,sb.length());
    }

113 路径总和2

List<List<Integer>> ans=new LinkedList<>();
    List<Integer> path=new LinkedList<>();
    public List<List<Integer>> pathSum(TreeNode root, int targetSum) {
        dfs(root,targetSum);
        return ans;
    }
    void dfs(TreeNode root,int targetSum){
        if (root==null) return;
        path.add(root.val);
        targetSum-=root.val;
        if(root.left==null && root.right==null && targetSum==0) {
            //ans.add(path);不行,后序修改path的时候会继续修改这个path的引用
            ans.add(new LinkedList<Integer>(path));
        }
        dfs(root.left,targetSum);
        dfs(root.right,targetSum);
        path.removeLast();
    }

958 二叉树的完全性检验

    public boolean isCompleteTree(TreeNode root) {
        if (root==null) return true;
        Queue<TreeNode> pq=new LinkedList<>();
        pq.add(root);
        boolean flag=false;
        while (!pq.isEmpty()){
            int sz=pq.size();
            for (int i=0;i<sz;i++){
                TreeNode node=pq.poll();
                if (node.left==null && node.right!=null) return false;
                if((node.left!=null || node.right!=null)&& flag) return false;
                //出现过空节点后后序节点都不能再有子节点
                if(node.left==null || node.right==null) flag=true;
                if (node.left!=null) pq.add(node.left);
                if (node.right!=null) pq.add(node.right);
            }
        }
        return true;
    }

LCR 174 寻找二叉搜索树中的目标节点

    int ans,cnt;
    public int findTargetNode(TreeNode root, int cnt) {
        if (root==null) return 0;
        this.cnt=cnt;
        dfs(root);
        return ans;
    }
    void dfs(TreeNode root){
        if (root==null) return;
        dfs(root.right);
        cnt--;
        if(cnt==0){
            ans=root.val;
        }
        dfs(root.left);
    }

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值