面试题-树(持续更新)

作者:disappearedgod
时间:2014-4-25

前记

这里主要列举一些简单的关于树的练习题。
正文部分是题目的叙述,点击标题(红色为可点击)会链接到解法。

目录

求二叉树中节点的最大距离
                                    《编程之美-3.8》 P241
重建二叉树
                                  《剑指offer-面试题6》P55  《编程之美-3.9》P246
分层遍历二叉树
                                  《编程之美-3.10》P252
树的子结构
                                  剑指offer-面试题18》P117
二叉树的镜像                                                                    
                                 剑指offer-面试题19》P125
从上往下打印二叉树  
                                 《剑指offer-面试题23》 P137
二叉搜索树的后序遍历序列
                                 《剑指offer-面试题24》P140
二叉树中和为某一值的路径 
                                 《剑指offer-面试题6》P143
二叉搜索树与双向链表  
                                 《剑指offer-面试题27》P151
二叉树的深度            
                                 《剑指offer-面试题39》P207
LeetCode-102Binary Tree Level Order Traversal
LeetCode-107 Binary Tree Level Order Traversal II
LeetCode-98    Validate Binary Search Tree
LeetCode-99    Recover Binary Search Tree
LeetCode-100 Same Tree
LeetCode-101 Symmetric Tree
LeetCode-104 Maximum Depth of Binary Tree
LeetCode-108 Convert Sorted Array to Binary Search Tree
LeetCode-110 Balanced Binary Tree
LeetCode-111 Minimum Depth of Binary Tree
LeetCode-112 Path Sum
LeetCode-113 Path Sum II
LeetCode-116 Populating Next Right Pointers in Each Node
LeetCode-117 Populating Next Right Pointers in Each Node II
LeetCode-124 Binary Tree Maximum Path Sum
LeetCode-129 Sum Root to Leaf Numbers
LeetCode-95    Unique Binary Search Trees II
LeetCode-96    Unique Binary Search Trees
LeetCode-94    Binary Tree Inorder Traversal
LeetCode-114 Flatten Binary Tree to Linked List
LeetCode-103 Binary Tree Zigzag Level Order Traversal
LeetCode-105 Construct Binary Tree from Preorder and Inorder Tree
LeetCode-106 Construct Binary Tree from Inorder and Postorder Tree

正文

求二叉树中节点的最大距离

                                   《编程之美-3.8》 P241
如果我们把二叉树看成一个图,父子节点之间的连线看成是双向的,我们姑且定义“距离”为两节点之间边的个数。
写一个程序求一棵二叉树中相距最远的两个节点之间的距离。
GVEdit
digraph G{        1-> 2;1-> 3;         2->4;2->5; 3->6; 3->7; 4->A; 6->B }



重建二叉树

                                 《剑指offer-面试题6》P55  《编程之美-3.9》P246
给出二叉树的三种遍历次序(前序,中序,后序),如果知道了遍历的结果,能不能把一棵树重新构造出来呢?
struct NODE{
  NODE* pLeft;
  NODE* pRight;
  char chValue; //it can be other data type
}
void Rebuild(char* pPreOrder, char*pInOrder, int nTreeLen, NODE** pRoot)
假如已经有了前序遍历和中序遍历的结果,希望通过一个算法重建。
example
前序遍历:a b d c e f
中序遍历:d b a e c f

GVEdit
digraph G{        a-> b;a-> c;         c->e;c->f; b->d; }


分层遍历二叉树

                                  《编程之美-3.10》P252
给定一棵二叉树,压球按分层遍历该二叉树,即从上到下按层次访问该二叉树(每一层将单独输出一行),每一层要求访问的顺序为从左到右,并将节点依次编号。分层输出二叉树。
example:

digraph G{        1-> 2;1-> 3;         2->4;2->5; 3->6; 5->7;5->8; }

输出为:
1
2 3
4 5 6
7 8
并写出另外一个函数,打印二叉树中某层次的节点(从左到右),其中根节点为第0层,函数原型为int PrintNodeAtLevel(Node* root, int level),成功返回1,失败则返回0.

树的子结构

                                  《剑指offer-面试题18》P117
输入两颗二叉树A和B,判断B是不是A的子结构。
二叉树定义如下:
struct BinaryTreeNode
{
  int m_nValue;
  BinaryTreeNode* m_pLeft;
  BinaryTreeNode* m_pRight;
}


二叉树的镜像                                                                    

                                 《剑指offer-面试题19》P125
请完成一个函数,输入一个二叉树,该函数输出它的镜像。
二叉树定义如下:
struct BinaryTreeNode
{
  int m_nValue;
  BinaryTreeNode* m_pLeft;
  BinaryTreeNode* m_pRight;
}


从上往下打印二叉树  

                                 《剑指offer-面试题23》 P137
从上往下打印出二叉树的每个节点,同一层的节点按照从左到右的顺序打印。
二叉树定义如下:
struct BinaryTreeNode
{
  int m_nValue;
  BinaryTreeNode* m_pLeft;
  BinaryTreeNode* m_pRight;
}
题目与《编程之美》层次打印题目一样。

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

                                 《剑指offer-面试题24》P140
输入一个整数数组,判断该数组是不是某二叉搜索树的后序遍历的结果。如果是返回true,否则返回false。假设输入的数组的任意两个数组都互不相同。

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

                                 《剑指offer-面试题6》P143
输入一棵二叉树和一个整数,打印出二叉树中节点值的和胃输入整数的所有路径。从树的根节点开始往下一直到叶节点所经历的节点形成一条路径。
二叉树定义如下:
struct BinaryTreeNode
{
  int m_nValue;
  BinaryTreeNode* m_pLeft;
  BinaryTreeNode* m_pRight;
}


二叉搜索树与双向链表  

                                 《剑指offer-面试题27》P151
输入一棵二叉搜索树,将该二叉搜索树转换成一个排序的双向链表。要求不能创建任何新的节点,职能调整数中节点指针的指向。
二叉树定义如下:
struct BinaryTreeNode
{
  int m_nValue;
  BinaryTreeNode* m_pLeft;
  BinaryTreeNode* m_pRight;
}


GVEdit
digraph G{        10-> 6;10-> 14;         6->4;6->8; 14->12; 14->16; }

digraph G{        4->6;6->4;    8->6;6->8;   8->10;10->8;    10->12;12->10;   14->12;12->14;    14->16;16->14; }


二叉树的深度            

                                 《剑指offer-面试题39》P207
输入一棵二叉树的根节点,求该树的深度。从根节点到叶节点一次经过的节点(含根、叶节点)形成树的一条路径,最长路径的长度为树的深度。
二叉树定义如下:
struct BinaryTreeNode
{
  int m_nValue;
  BinaryTreeNode* m_pLeft;
  BinaryTreeNode* m_pRight;
}

题目二:输入一棵二叉树的根节点,判断该树是不是平衡二叉树。如果某二叉树中任意节点的左右子树的深度相差不超过1,那么它就是一棵平衡二叉树。

LeetCode-102 Binary Tree Level Order Traversal


LeetCode-107 Binary Tree Level Order Traversal II


LeetCode-98    Validate Binary Search Tree


LeetCode-99    Recover Binary Search Tree


LeetCode-100Same Tree

Same Tree

  Total Accepted: 16148  Total Submissions: 38995 My Submissions

Given two binary trees, write a function to check if they are equal or not.

Two binary trees are considered equal if they are structurally identical and the nodes have the same value.

/**
 * Definition for binary tree
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode(int x) { val = x; }
 * }
 */



LeetCode-101Symmetric Tree

Symmetric Tree

  Total Accepted: 12814  Total Submissions: 40358 My Submissions

Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center).

For example, this binary tree is symmetric:

    1
   / \
  2   2
 / \ / \
3  4 4  3

But the following is not:

    1
   / \
  2   2
   \   \
   3    3

Note:
Bonus points if you could solve it both recursively and iteratively.

confused what "{1,#,2,3}" means? > read more on how binary tree is serialized on OJ.


OJ's Binary Tree Serialization:

The serialization of a binary tree follows a level order traversal, where '#' signifies a path terminator where no node exists below.

Here's an example:

   1
  / \
 2   3
    /
   4
    \
     5
The above binary tree is serialized as  "{1,2,3,#,#,4,#,#,5}".


LeetCode-104 Maximum Depth of Binary Tree


LeetCode-108 Convert Sorted Array to Binary Search Tree


LeetCode-110 Balanced Binary Tree


LeetCode-111 Minimum Depth of Binary Tree


LeetCode-112 Path Sum


LeetCode-113 Path Sum II


LeetCode-116 Populating Next Right Pointers in Each Node


LeetCode-117 Populating Next Right Pointers in Each Node II


LeetCode-124 Binary Tree Maximum Path Sum


LeetCode-129 Sum Root to Leaf Numbers


LeetCode-95    Unique Binary Search Trees II


LeetCode-96    Unique Binary Search Trees


LeetCode-94    Binary Tree Inorder Traversal


LeetCode-114 Flatten Binary Tree to Linked List


LeetCode-103 Binary Tree Zigzag Level Order Traversal


LeetCode-105 Construct Binary Tree from Preorder and Inorder Tree


LeetCode-106 Construct Binary Tree from Inorder and Postorder Tree


参考

  • 《剑指offer》何海涛
  • 《编程之美》邹欣等
                                  
                                  
                                  
                                  
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值