吾爱破解 - LCG - LSG |安卓破解|病毒分析|www.52pojie.cn

 找回密码
 注册[Register]

QQ登录

只需一步,快速开始

查看: 1320|回复: 2
收起左侧

[Java 转载] 【面试】算法题之二叉树右视图(并构建二叉树)

[复制链接]
落神 发表于 2021-1-12 17:03
本帖最后由 落神 于 2021-1-12 17:13 编辑

看见很多大佬分享了一些算法,最近自己也在准备找实习,也来发一发。

构建二叉树:迭代
右视图:BFS+队列


[Java] 纯文本查看 复制代码
import java.util.*;

class TreeNode{
    int val;
    TreeNode left;
    TreeNode right;
    public TreeNode(int val){
        this.val = val;
    }
}

public class Solution {
    /**
     * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
     * 求二叉树的右视图
     * xianxu int整型一维数组 先序遍历
     * zhongxu int整型一维数组 中序遍历
     * int整型一维数组
     */
    public int[] solve (int[] xianxu, int[] zhongxu) {
        // write code here
        if(xianxu.length == 0 || zhongxu.length == 0)
            return null;
        //构建二叉树
        TreeNode root = buildTree(xianxu,zhongxu);
        //右视图  层次遍历 只保存最后一个节点
        ArrayList<Integer> list = new ArrayList<>();
        Queue<TreeNode> queue = new ArrayDeque<>();
        queue.add(root);
        while(!queue.isEmpty()){
            int size = queue.size();
            for(int i=0;i<size;i++){
                TreeNode node = queue.poll();
                if(i == size-1)
                    list.add(node.val);
                if(node.left != null)
                    queue.add(node.left);
                if(node.right != null)
                    queue.add(node.right);
            }
        }
        int[] result = new int[list.size()];
        for(int i=0;i<list.size();i++)
            result[i] = list.get(i);
        return result;
    }
    
    public TreeNode buildTree(int[] xianxu, int[] zhongxu){
        //双指针 
        int pre = 0;
        int mid = 0;
        //当前节点
        TreeNode curRoot = new TreeNode(xianxu[pre]);
        //root作为根节点 返回
        TreeNode root = curRoot;
        Stack<TreeNode> stack = new Stack<>();
        //入栈
        stack.push(curRoot);
        pre++;
        while(pre < xianxu.length){
            //当前节点 = 中序遍历的节点时候,证明下一个节点是右子树的节点
            if(curRoot.val == zhongxu[mid]){
                //出栈 与中序遍历匹配 不匹配时证明先序遍历的值是当前节点的右节点 
                while(!stack.isEmpty() && stack.peek().val == zhongxu[mid]){
                    curRoot = stack.pop();
                    //mid同步,更新到下一个右节点判断
                    mid++;
                }
                //设置右节点
                curRoot.right = new TreeNode(xianxu[pre]);
                //更新节点
                curRoot = curRoot.right;
                //入栈
                stack.push(curRoot);
                pre++;
            }else{
                //设置左节点
                curRoot.left = new TreeNode(xianxu[pre]);
                curRoot = curRoot.left;
                stack.push(curRoot);
                pre++;
            }
        }
        return root;
    }
}

免费评分

参与人数 1吾爱币 +5 热心值 +1 收起 理由
苏紫方璇 + 5 + 1 欢迎分析讨论交流,吾爱破解论坛有你更精彩!

查看全部评分

发帖前要善用论坛搜索功能,那里可能会有你要找的答案或者已经有人发布过相同内容了,请勿重复发帖。

Newman616 发表于 2021-3-19 10:27
学习了,感谢帖主分享
您需要登录后才可以回帖 登录 | 注册[Register]

本版积分规则 警告:本版块禁止灌水或回复与主题无关内容,违者重罚!

快速回复 收藏帖子 返回列表 搜索

RSS订阅|小黑屋|处罚记录|联系我们|吾爱破解 - LCG - LSG ( 京ICP备16042023号 | 京公网安备 11010502030087号 )

GMT+8, 2024-5-11 00:44

Powered by Discuz!

Copyright © 2001-2020, Tencent Cloud.

快速回复 返回顶部 返回列表