前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >【数据结构与算法】一起搞定面试中的二叉树题目(二)

【数据结构与算法】一起搞定面试中的二叉树题目(二)

作者头像
用户1634449
发布2018-10-18 16:15:03
5260
发布2018-10-18 16:15:03
举报
文章被收录于专栏:Python专栏Python专栏

作者:IOExceptioner

本文继续一起搞定面试中的二叉树(一)一文总结二叉树相关的面试题。

12. 二叉树的前序遍历

迭代解法

代码语言:javascript
复制
ArrayList<Integer> preOrder(TreeNode root){
    Stack<TreeNode> stack = new Stack<TreeNode>();
    ArrayList<Integer> list = new ArrayList<Integer>();
   if(root == null){
        return list;
    }
    stack.push(root);
    while(!stack.empty()){
        TreeNode node = stack.pop();
        list.add(node.val);
        if(node.right!=null){
            stack.push(node.right);
        }
        if(node.left != null){
            stack.push(node.left);
        }
    }
    return list;
}

递归解法

代码语言:javascript
复制
ArrayList<Integer> preOrderReverse(TreeNode root){
   ArrayList<Integer> result = new ArrayList<Integer>();
    preOrder2(root,result);
    return result;
}
void preOrder2(TreeNode root,ArrayList<Integer> result){
    if(root == null){
        return;
    }
    result.add(root.val);
    preOrder2(root.left,result);
    preOrder2(root.right,result);
}

13. 二叉树的中序遍历

代码语言:javascript
复制
ArrayList<Integer> inOrder(TreeNode root){
    ArrayList<Integer> list = new ArrayList<<Integer>();
    Stack<TreeNode> stack = new Stack<TreeNode>();
    TreeNode current = root;
    while(current != null|| !stack.empty()){
         while(current != null){
             stack.add(current);
             current = current.left;
         }
         current = stack.peek();
         stack.pop();
         list.add(current.val);
         current = current.right;
    }
    return list;
}

14.二叉树的后序遍历

代码语言:javascript
复制
ArrayList<Integer> postOrder(TreeNode root){
    ArrayList<Integer> list = new ArrayList<Integer>();
    if(root == null){
        return list;
    }
    list.addAll(postOrder(root.left));
    list.addAll(postOrder(root.right));
    list.add(root.val);
    return list;
}

15.前序遍历和后序遍历构造二叉树

代码语言:javascript
复制
TreeNode buildTreeNode(int[] preorder,int[] inorder){
   if(preorder.length!=inorder.length){
        return null;
    }
    return myBuildTree(inorder,0,inorder.length-1,preorder,0,preorder.length-1);
}

TreeNode myBuildTree(int[] inorder,int instart,int inend,int[] preorder,int prestart,int preend){
    if(instart>inend){
        return null;
    }
    TreeNode root = new TreeNode(preorder[prestart]);
    int position = findPosition(inorder,instart,inend,preorder[start]);
    root.left = myBuildTree(inorder,instart,position-1,preorder,prestart+1,prestart+position-instart);
    root.right = myBuildTree(inorder,position+1,inend,preorder,position-inend+preend+1,preend);
    return root;
}

int findPosition(int[] arr,int start,int end,int key){
   int i;
    for(i = start;i<=end;i++){
        if(arr[i] == key){
            return i;
        }
     }
     return -1;
 }

16.在二叉树中插入节点

代码语言:javascript
复制
TreeNode insertNode(TreeNode root,TreeNode node){
    if(root == node){
        return node;
    }
    TreeNode tmp = new TreeNode();
    tmp = root;
    TreeNode last = null;
    while(tmp!=null){
         last = tmp;
         if(tmp.val>node.val){
             tmp = tmp.left;
         }else{
             tmp = tmp.right;
         }
    }
    if(last!=null){
         if(last.val>node.val){
             last.left = node;
         }else{
             last.right = node;
         }
     }
     return root;
}

17.输入一个二叉树和一个整数,打印出二叉树中节点值的和等于输入整数所有的路径

代码语言:javascript
复制
void findPath(TreeNode r,int i){
    if(root == null){
         return;
     }
     Stack<Integer> stack = new Stack<Integer>();
     int currentSum = 0;
     findPath(r, i, stack, currentSum);
}

void findPath(TreeNode r,int i,Stack<Integer> stack,int currentSum){
    currentSum+=r.val;
    stack.push(r.val);
    if(r.left==null&&r.right==null){
         if(currentSum==i){
              for(int path:stack){
                  System.out.println(path);
              }
         }
     }
     if(r.left!=null){
          findPath(r.left, i, stack, currentSum);
     }
     if(r.right!=null){
          findPath(r.right, i, stack, currentSum);
     }
     stack.pop();
 }

18.二叉树的搜索区间

给定两个值 k1 和 k2(k1 < k2)和一个二叉查找树的根节点。找到树中所有值在 k1 到 k2 范围内的节点。即打印所有x (k1 <= x <= k2) 其中 x 是二叉查找树的中的节点值。返回所有升序的节点值。

代码语言:javascript
复制
ArrayList<Integer> result;
ArrayList<Integer> searchRange(TreeNode root,int k1,int k2){
    result = new ArrayList<Integer>();
    searchHelper(root,k1,k2);
    return result;
}

void searchHelper(TreeNode root,int k1,int k2){
    if(root == null){
        return;
    }
    if(root.val>k1){
        searchHelper(root.left,k1,k2);
    }
    if(root.val>=k1&&root.val<=k2){
        result.add(root.val);
    }
    if(root.val<k2){
        searchHelper(root.right,k1,k2);
    }
}

19.二叉树的层次遍历

代码语言:javascript
复制
ArrayList<ArrayList<Integer>> levelOrder(TreeNode root){
   ArrayList<ArrayList<Integer>> result = new ArrayList<ArrayList<Integer>>();
    if(root == null){
        return result;
    }
    Queue<TreeNode> queue = new LinkedList<TreeNode>();
    queue.offer(root);
    while(!queue.isEmpty()){
         int size = queue.size();
         ArrayList<<Integer> level = new ArrayList<Integer>():
         for(int i = 0;i < size ;i++){
             TreeNode node = queue.poll();
             level.add(node.val);
             if(node.left != null){
                 queue.offer(node.left);
             }
             if(node.right != null){
                 queue.offer(node.right);
             }
          } 
          result.add(Level);
     }
     return result;
}

20.二叉树内两个节点的最长距离

二叉树中两个节点的最长距离可能有三种情况: 1.左子树的最大深度+右子树的最大深度为二叉树的最长距离 2.左子树中的最长距离即为二叉树的最长距离 3.右子树种的最长距离即为二叉树的最长距离 因此,递归求解即可

代码语言:javascript
复制
private static class Result{  
    int maxDistance;  
    int maxDepth;  
    public Result() {  
    }  
    public Result(int maxDistance, int maxDepth) {  
        this.maxDistance = maxDistance;  
        this.maxDepth = maxDepth;  
    }  
}  

int getMaxDistance(TreeNode root){
   return getMaxDistanceResult(root).maxDistance;
}

Result getMaxDistanceResult(TreeNode root){
   if(root == null){
        Result empty = new Result(0,-1);
        return empty;
    }
    Result lmd = getMaxDistanceResult(root.left);
    Result rmd = getMaxDistanceResult(root.right);
    Result result = new Result();
    result.maxDepth = Math.max(lmd.maxDepth,rmd.maxDepth) + 1;
    result.maxDistance = Math.max(lmd.maxDepth + rmd.maxDepth,Math.max(lmd.maxDistance,rmd.maxDistance));
    return result;
}

21.不同的二叉树

给出 n,问由 1…n 为节点组成的不同的二叉查找树有多少种?

代码语言:javascript
复制
int numTrees(int n ){
    int[] counts = new int[n+2];
    counts[0] = 1;
    counts[1] = 1;
    for(int i = 2;i<=n;i++){
        for(int j = 0;j<i;j++){
            counts[i] += counts[j] * counts[i-j-1];
        }
    }
    return counts[n];
}

22.判断二叉树是否是合法的二叉查找树(BST)

一棵BST定义为: 节点的左子树中的值要严格小于该节点的值。 节点的右子树中的值要严格大于该节点的值。 左右子树也必须是二叉查找树。 一个节点的树也是二叉查找树。

代码语言:javascript
复制
public int lastVal = Integer.MAX_VALUE;
public boolean firstNode = true;
public boolean isValidBST(TreeNode root) {
   // write your code here
    if(root==null){
        return true;
    }
    if(!isValidBST(root.left)){
        return false;
    }
    if(!firstNode&&lastVal >= root.val){
        return false;
    }
    firstNode = false;
    lastVal = root.val;
    if (!isValidBST(root.right)) {
         return false;
    }
    return true;
}

深刻的理解这些题的解法思路,在面试中的二叉树题目就应该没有什么问题。

本文参与 腾讯云自媒体分享计划,分享自微信公众号。
原始发表:2018-10-02,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 Python专栏 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档