二叉树之牛客网篇

重建二叉树

题目链接:https://www.nowcoder.com/practice/8a19cbe657394eeaac2f6ea9b0f6fcf6?tpId=13&tqId=11157&rp=1&ru=/ta/coding-interviews&qru=/ta/coding-interviews/question-ranking

输入某二叉树的前序遍历和中序遍历的结果,请重建出该二叉树。假设输入的前序遍历和中序遍历的结果中都不含重复的数字。例如输入前序遍历序列{1,2,4,7,3,5,6,8}和中序遍历序列{4,7,2,1,5,3,8,6},则重建二叉树并返回。

import java.util.Arrays;

public class Solution{

 public TreeNode reConstructBinaryTree(int [] pre,int [] in) {

     if(pre.length==0||in.length==0)
         return null;


     int root=pre[0];

     TreeNode node=new TreeNode(root);

     int index=-1;
     for(int i=0;i<in.length;i++){

         if(in[i]==root){
             index=i;
             break;
         }         

     }
     if(index==-1)
          return null;

     node.left=reConstructBinaryTree(Arrays.copyOfRange(pre, 1, index+1),Arrays.copyOfRange(in, 0, index));
     node.right=reConstructBinaryTree(Arrays.copyOfRange(pre, index+1, pre.length),Arrays.copyOfRange(in, index+1, in.length));

    return node;

    }


}

树的子结构

题目链接https://www.nowcoder.com/practice/6e196c44c7004d15b1610b9afca8bd88?tpId=13&tqId=11170&rp=1&ru=/ta/coding-interviews&qru=/ta/coding-interviews/question-ranking

输入两颗二叉树A,B,判断B是不是A的子结构。

 class TreeNode {
    int val = 0;
    TreeNode left = null;
    TreeNode right = null;

    public TreeNode(int val) {
        this.val = val;

    }

}

public class Solution {
    public boolean HasSubtree(TreeNode root1,TreeNode root2) {
        if(root1==null)
            return false;
        if(root2==null)
            return false;//当为root2为空的时候,不算子结构
        return isSubTree(root1,root2)||HasSubtree(root1.left,root2)||HasSubtree(root1.right,root2);

    }

    public boolean isSubTree(TreeNode root1,TreeNode root2){
        if(root2==null)
            return true;
        if(root1==null)
            return false;
        if(root1.val==root2.val)
            return isSubTree(root1.left,root2.left)&&isSubTree(root1.right,root2.right);

        return false;


    }
}

二叉树镜像

问题:操作给定的二叉树,将其变换为源二叉树的镜像。 输入描述: 二叉树的镜像定义:源二叉树 8 / \ 6 10 / \ / \ 5 7 9 11 镜像二叉树 8 / \ 10 6 / \ / \ 11 9 7 5

题目链接: https://www.nowcoder.com/practice/564f4c26aa584921bc75623e48ca3011?tpId=13&tqId=11171&rp=1&ru=/ta/coding-interviews&qru=/ta/coding-interviews/question-ranking

    public void Mirror(TreeNode root) {

        if(root==null)
            return;

        TreeNode right=root.right;
        TreeNode left=root.left;

        root.left=right;
        root.right=left;
        Mirror(root.left);
        Mirror(root.right);


    }

二叉搜索树的后序遍历序列

问题:输入一个整数数组,判断该数组是不是某二叉搜索树的后序遍历的结果。如果是则输出Yes,否则输出No。假设输入的数组的任意两个数字都互不相同。 http://www.nowcoder.com/practice/a861533d45854474ac791d90e447bafd?tpId=13&tqId=11176&rp=2&ru=/ta/coding-interviews&qru=/ta/coding-interviews/question-ranking

import java.util.Arrays;
public class Solution {
    public boolean VerifySquenceOfBST(int [] sequence) {

        if(sequence==null||sequence.length==0)
            return false;


        return this.isBST(sequence,0,sequence.length-1);

    }


   public boolean isBST(int[] arr,int start,int end){

       if(start>=end)
           return true;


        int current=arr[end];//根


        int i=start;

        while(i<end&&arr[i]<current){

            i++;//找到左右子树分界点
        }
        for(int j=i;j<end;j++){
            if(arr[j]<current)
                return false;//若右子树中有小于根结点的则false
        }

        return isBST(arr,start,i-1)&&isBST(arr,i,end-1);
   }
}

层次遍历

问题:从上往下打印出二叉树的每个节点,同层节点从左至右打印。

import java.util.ArrayList;
import java.util.Queue;
import java.util.LinkedList;
/**
public class TreeNode {
    int val = 0;
    TreeNode left = null;
    TreeNode right = null;

    public TreeNode(int val) {
        this.val = val;

    }

}
*/
public class Solution {
    public ArrayList<Integer> PrintFromTopToBottom(TreeNode root) {

        //!注意,不能返回null,应该返回list  !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

        //if(root==null)
           // return null;

        Queue<TreeNode> queue=new LinkedList<TreeNode>();
        ArrayList<Integer> list=new ArrayList<Integer>();

        if(root==null)
            return list;
        queue.offer(root);

        while(!queue.isEmpty()){

            TreeNode node=queue.poll();
            list.add(node.val);
            if(node.left!=null)
                queue.offer(node.left);
            if(node.right!=null)
                queue.offer(node.right);
        }
        return list;

    }
}

二叉树中和为某一值的路径

题目描述

输入一颗二叉树和一个整数,打印出二叉树中结点值的和为输入整数的所有路径。路径定义为从树的根结点开始往下一直到叶结点所经过的结点形成一条路径。 https://www.nowcoder.com/practice/b736e784e3e34731af99065031301bca?tpId=13&tqId=11177&rp=2&ru=/ta/coding-interviews&qru=/ta/coding-interviews/question-ranking

import java.util.ArrayList;
/**
public class TreeNode {
    int val = 0;
    TreeNode left = null;
    TreeNode right = null;

    public TreeNode(int val) {
        this.val = val;

    }

}
*/
public class Solution {

    private ArrayList<ArrayList<Integer>> arraylist=new ArrayList<ArrayList<Integer>>();
    private ArrayList<Integer> list=new ArrayList<Integer>();

    public ArrayList<ArrayList<Integer>> FindPath(TreeNode root,int target) {
        if(root==null)
            return arraylist;
        list.add(root.val);
        target-=root.val;
        if(target==0&&root.left==null&&root.right==null)
            arraylist.add(new ArrayList<Integer>(list));//注意这里

        FindPath(root.left,target);
        FindPath(root.right,target);

        list.remove(list.size()-1);

        return arraylist;

    }


}

判断是否是平衡二叉树

输入一棵二叉树,判断该二叉树是否是平衡二叉树。 https://www.nowcoder.com/practice/8b3b95850edb4115918ecebdf1b4d222?tpId=13&tqId=11192&rp=2&ru=%2Fta%2Fcoding-interviews&qru=%2Fta%2Fcoding-interviews%2Fquestion-ranking

public class Solution {
    public boolean IsBalanced_Solution(TreeNode root) {

        if(root==null)
            return true;

        int flag=Math.abs(TreeDepth(root.left)-TreeDepth(root.right));

        if(flag>1)
            return false;

    return  IsBalanced_Solution(root.left)&&IsBalanced_Solution(root.right);




    }


    public int TreeDepth(TreeNode root){

        if(root==null)
            return 0;
        int depth;
        depth=1+Math.max(TreeDepth(root.left),TreeDepth(root.right));

        return depth;
    }
}

二叉搜索树转为双向链表

问题描述 输入一棵二叉搜索树,将该二叉搜索树转换成一个排序的双向链表。要求不能创建任何新的结点,只能调整树中结点指针的指向。

/**
public class TreeNode {
    int val = 0;
    TreeNode left = null;
    TreeNode right = null;

    public TreeNode(int val) {
        this.val = val;

    }

}
*/
public class Solution {
    TreeNode phead;  //指针
    TreeNode realhead;  //头指针
    public TreeNode Convert(TreeNode pRootOfTree) {

        if(pRootOfTree==null)
            return pRootOfTree;

        ConvertSub(pRootOfTree);

        return realhead;
    }

    //中序遍历
    public void ConvertSub(TreeNode root){
        if(root==null)
            return ;
        ConvertSub(root.left);
        if(phead==null){
            phead=root;
            realhead=root;
        }
        else{
            phead.right=root;
            root.left=phead;
            phead=root;
        }
        ConvertSub(root.right);
    }


}

results matching ""

    No results matching ""