练习(3)

一、重建二叉树

输入某二叉树的谦虚比哪里和中序遍历的结果,请重建该而擦函数。假设输入的前序遍历和中序遍历的结果中都不含重复的数字。例如输入前序遍历{1,2,4,5,3,5,6,8}和中序序列{,4,7,2,1,5,3,8,6},重建该二叉树

解:前序遍历的第一个值为根结点的值,使用这个值将中序遍历分成两部分,左部分为树的左子树中序遍历结果,右部分为树的右子树中序遍历的结果。每次在前序遍历中找根节点并创建一个根节点,然后在中序遍历中确定根节点位置,并确定当前根节点的左右子树,然后以同样的方法去构建左右子树

链接:https://www.nowcoder.com/questionTerminal/8a19cbe657394eeaac2f6ea9b0f6fcf6
来源:牛客网

public class Solution {
    public TreeNode reConstructBinaryTree(int [] pre,int [] in) {
        TreeNode root=reConstructBinaryTree(pre,0,pre.length-1,in,0,in.length-1);
        return root;
    }
    //前序遍历{1,2,4,7,3,5,6,8}和中序遍历序列{4,7,2,1,5,3,8,6}
    private TreeNode reConstructBinaryTree(int [] pre,int startPre,int endPre,int [] in,int startIn,int endIn) {
         
        if(startPre>endPre||startIn>endIn)
            return null;
        TreeNode root=new TreeNode(pre[startPre]);
         
        for(int i=startIn;i<=endIn;i++)
            if(in[i]==pre[startPre]){
                root.left=reConstructBinaryTree(pre,startPre+1,startPre+i-startIn,in,startIn,i-1);
                root.right=reConstructBinaryTree(pre,i-startIn+startPre+1,endPre,in,i+1,endIn);
                      break;
            }
                 
        return root;
    }
}

二、二叉树的下一个结点

给定二叉树和其中一个结点,请找出中序遍历顺序的下一个结点并返回。注意,树中的结点不仅包含左右子结点,同时包含指向父结点的指针

思路:

(1)当x有右儿子:y为x的右子树的最左边结点
(2)当x无右儿子:
           a. x是x的父结点的左儿子:y为x的父结点
           b. x是x的父结点的右儿子:
                                   <1>若父结点是爷结点的左儿子:y为x的爷结点

                                   <2>否则为NULL,到达树的中序遍历的末尾

链接:https://www.nowcoder.com/questionTerminal/9023a0c988684a53960365b889ceaf5e
来源:牛客网

public class Solution {
    TreeLinkNode GetNext(TreeLinkNode node)
    {
        if(node==null) return null;
        if(node.right!=null){    //如果有右子树,则找右子树的最左节点
            node = node.right;
            while(node.left!=null) node = node.left;
            return node;
        }
        while(node.next!=null){ //没右子树,则找第一个当前节点是父节点左孩子的节点
            if(node.next.left==node) return node.next;
            node = node.next;
        }
        return null;   //退到了根节点仍没找到,则返回null
    }
}

三、对称的二叉树

实现函数检验二叉树是否对称

思路:

只要采用前序、中序、后序、层次遍历等任何一种遍历方法,分为先左后右和先

右后左两种方法,只要两次结果相等就说明这棵树是一颗对称二叉树。比如,前序,根左右,根右左,如果对应元素相同则为对称。

递归

1.只要pRoot.left和pRoot.right是否对称即可

2.左右节点的值相等对称子树left.left, right.right ;left.rigth,right.left也对称

链接:https://www.nowcoder.com/questionTerminal/ff05d44dfdb04e1d83bdbdab320efbcb
来源:牛客网

boolean isSymmetrical(TreeNode pRoot)
    {
        if(pRoot == null) return true;
        return isSymmetrical(pRoot.left, pRoot.right);
    }
    private boolean isSymmetrical(TreeNode left, TreeNode right) {
        if(left == null && right == null) return true;
        if(left == null || right == null) return false;
        return left.val == right.val //为镜像的条件:左右节点值相等
                && isSymmetrical(left.left, right.right) //2.对称的子树也是镜像
                && isSymmetrical(left.right, right.left);
    }

 
DFS使用stack来保存成对的节点

  1.出栈的时候也是成对成对的 ,

1.若都为空,继续;

2.一个为空,返回false;

3.不为空,比较当前值,值不等,返回false;

  2.确定入栈顺序,每次入栈都是成对成对的,如left.left, right.right ;left.rigth,right.left
 

翻译练习:Juan Louris,a junior geology major,decided to give an informatiive speech about how earthquakes occur.From his audience analysis he learned that only 2 or 3 of his classmates knew much of anything about geology.

胡安·路易丝是一名地质专业大三学生,决定做一个关于地震发生内容丰富的演讲。从他听众的分析,他了解到仅有2或3个他的同学了解地质。

Juan realized then that he must present his speech at an elementary level and with a minimum of scientific language.As he prepared the speech,Juan kept asking himself,"How can I make ths clear and meaningful to someone who knows nothing about earthquakes or geological principles?"

胡安意识到他必须在 低层次和 少用科学语言下呈现他的演讲。当他准备演讲,胡安保持问他自己,“我怎样才能清晰,有意义的给那些对地震或地质原理不了解的人演讲”。

Since he was speaking in the Midwest,he decided to begin by nothing that the most severe earthquake in American history took place not in California or Alaska but at New Madrid,Missouri in 1811.If such an earthquake happened today,it would be felt  from the Rocky Mountains to the Atlantic Ocean and would flatten most of the cities in the Mississippi valley.

自从他在中西部演讲,他决定一开始从美国历史上在加利福尼亚州或阿拉斯加州发生的最严重地震说起 而不是1811在新马德里或密苏里州发生的地震。如果这样的地震发生在今天,那将会从落基山脉到大西洋都能感觉到,会压平密西西比河谷大多数城市。

That,he figured,should get his classmates'attention.Throughout the body of the speech,Juan dealt only with the basic mechanics of earthquakes,and carefully avoided technical terms.He also prepared visual aids,diagramming fault lines,so his classmates wouldn't get confused.To be absolutely safe,Juan asked his roomate,who was not a geology major,to listen to the speech.

接着,他表明,应当获取他同学的注意。贯穿整个演讲,胡安解释基础地震力学,尽量的避免术语。他也准备了视觉辅助,绘制故障线图,因此他的同学不感到困惑。为了万无一失,胡安要求他非地质专业的室友,去听演讲。

 “Stop me”,he said,"any time I say something you don't understand."Juan's roommate stopped him four times.And at each spot,Juan worked out a way to make his point more clearly.Finally,he had a speech that was interesting and perfectly understandable to his audience.

"打断我",他说,“每当我说的你听不懂的时候”。胡安的室友打断他四次。在每个地方,胡安想办法使他的观点更清晰。最终,他的演讲是有趣和完美的 ,他的听众能理解。

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值