您的位置:首页 > 职场人生

面试笔试杂项积累-leetcode 101-105

2016-02-04 23:02 429 查看

101.101-Symmetric Tree-Difficulty: Easy

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.

思路

检测是否为镜像二叉树

博主用中序遍历来做的,两头往中间是一样的,但是忽略了一种极其重要的情况,中序遍历真的是很有欺骗性,

I think in-order traversal is not the problem itself. A simple serialization doesn't capture the structure of the tree which is important in determining symmetry. For instance the following 2 trees in your solution would be serialized
into the same string but they are not the same tree and have different symmetry.

1
/
2
/
1

2
/   \
1       1
[/code]
I haven't verified this but if you use place holder for empty node and always output a string that represents a "full" tree it might work. It might be harder than it seems.

12#1###

121
[/code] https://leetcode.com/discuss/15409/symmetric-tree-why-inorder-traversal-doesnt-work 找到了这个原因,博主又想了一个方法使中序遍历成为了可能,,,就是加上了他们的层数,再比较,镜像都是同一层的,你懂得

/**
* Definition for a binary tree node.
* public class TreeNode {
* public int val;
* public TreeNode left;
* public TreeNode right;
* public TreeNode(int x) { val = x; }
* }
*/
public class Solution {
IList<int> list = new List<int>();
public bool IsSymmetric(TreeNode root)
{
if (root == null)
return true;
if (root.left != null && root.right != null && root.left.val != root.right.val)
return false;
traverse1(root,0);
if (list.Count % 2 != 1)
return false;
int half = list.Count / 2;
if (list[half] != root.val)
return false;
for (int i = 0; i < half; i++)
{
if (list[i] != list[list.Count-1-i])
return false;
}
return true;
}
void traverse1(TreeNode root,int level)
{
if (root.left != null)
traverse1(root.left,level+1);
list.Add(root.val+level);
if (root.right != null)
traverse1(root.right,level+1);
}

}

102.102-Binary Tree Level Order Traversal-Difficulty: Easy

Given a binary tree, return the level order traversal of its nodes' values. (ie, from left to right, level by level).

For example:

Given binary tree
{3,9,20,#,#,15,7}
,

3
/ \
9  20
/  \
15   7

return its level order traversal as:

[
[3],
[9,20],
[15,7]
]

思路

层序遍历,

/**
* Definition for a binary tree node.
* public class TreeNode {
* public int val;
* public TreeNode left;
* public TreeNode right;
* public TreeNode(int x) { val = x; }
* }
*/
public class Solution {
IList<IList<int>> fin = new List<IList<int>>();
public IList<IList<int>> LevelOrder(TreeNode root)
{
if (root == null)
return fin;
fin.Add(new List<int>());
traverse(root, 0);
return fin;
}
void traverse(TreeNode root, int level)
{
if(fin.Count<level+1)
fin.Add(new List<int>());
if (root.left != null)
traverse(root.left, level + 1);
fin[level].Add(root.val);
if (root.right != null)
traverse(root.right, level + 1);
}
}

103.103-Binary Tree Zigzag Level Order Traversal-Difficulty: Medium

Given a binary tree, return the zigzag level order traversal of its nodes' values. (ie, from left to right, then right to left for the next level and alternate between).

For example:

Given binary tree
{3,9,20,#,#,15,7}
,

3
/ \
9  20
/  \
15   7

return its zigzag level order traversal as:

[
[3],
[20,9],
[15,7]
]

思路

对上一道题换汤不换药,依旧层序遍历

所谓的Zig-Zag 遍历,及第一行从左到右,第二行从右到左,第三行从左到右。。。

我们判断奇偶再考虑前插还是后插即可

/**
* Definition for a binary tree node.
* public class TreeNode {
* public int val;
* public TreeNode left;
* public TreeNode right;
* public TreeNode(int x) { val = x; }
* }
*/
public class Solution {
IList<IList<int>> fin = new List<IList<int>>();
public IList<IList<int>> ZigzagLevelOrder(TreeNode root)
{
if (root == null)
return fin;
fin.Add(new List<int>());
traverse(root, 0);
return fin;
}
void traverse(TreeNode root, int level)//所谓的Zig-Zag 遍历,及第一行从左到右,第二行从右到左,第三行从左到右,依次类推。
{
if (fin.Count < level + 1)
fin.Add(new List<int>());
if (root.left != null)
traverse(root.left, level + 1);
if (level%2 == 0)
fin[level].Add(root.val);
else
fin[level].Insert(0, root.val);
if (root.right != null)
traverse(root.right, level + 1);
}
}

104.104-Maximum Depth of Binary Tree-Difficulty: Easy

Given a binary tree, find its maximum depth.

The maximum depth is the number of nodes along the longest path from the root node down to the farthest leaf node.

思路

求二叉树的最长深度

/**
* Definition for a binary tree node.
* public class TreeNode {
* public int val;
* public TreeNode left;
* public TreeNode right;
* public TreeNode(int x) { val = x; }
* }
*/
public class Solution {
public int MaxDepth(TreeNode root) {
int deep = 0;
if (root != null)
{
int lDeep = MaxDepth(root.left);
int rDeep = MaxDepth(root.right);
deep = lDeep > rDeep ? lDeep + 1 : rDeep + 1;
}

return deep;
}
}

105.105-Maximum Depth of Binary Tree-Difficulty: Medium

Given preorder and inorder traversal of a tree, construct the binary tree.

Note:

You may assume that duplicates do not exist in the tree.

思路

给予先序和中序遍历的结果,返回构造的二叉树

参考: http://www.cnblogs.com/ganganloveu/p/4133267.html
/**
* Definition for a binary tree node.
* public class TreeNode {
* public int val;
* public TreeNode left;
* public TreeNode right;
* public TreeNode(int x) { val = x; }
* }
*/
public class Solution {
public TreeNode BuildTree(int[] preorder, int[] inorder) {
return Helper(preorder, 0, preorder.Length - 1, inorder, 0, inorder.Length - 1);
}

TreeNode Helper(int[] preorder, int begin1, int end1, int[] inorder, int begin2, int end2)
{
if (begin1 > end1)
return null;
else if (begin1 == end1)
return new TreeNode(preorder[begin1]);

TreeNode root = new TreeNode(preorder[begin1]);
int i = begin2;
for (; i <= end2; i++)
{
if (inorder[i] == preorder[begin1])
break;
}
int leftlen = i - begin2;
root.left = Helper(preorder, begin1 + 1, begin1 + leftlen, inorder, begin2, begin2 + leftlen - 1);
root.right = Helper(preorder, begin1 + leftlen + 1, end1, inorder, begin2 + leftlen + 1, end2);
return root;
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: