题目:二叉树中有两个节点对换了值,恢复他们。思路:因为中序遍历是有序的,如果中序遍历后的数组出现乱序,说明就是交换的。从前往后,第一次乱序的第一个,后最后一次乱序的后一个,然后把这两个值对换就好了。想了个非常挫的办法。先中序遍历Binary Tree Inorder Traversal,然后在数组中...
分类:
其他好文 时间:
2014-11-27 00:08:40
阅读次数:
240
问题描述:
Given a binary tree, return the inorder traversal of its nodes' values.
For example:
Given binary tree {1,#,2,3},
1
2
/
3
return [1,3,2].
Note: Recursive solution...
分类:
其他好文 时间:
2014-11-26 22:39:25
阅读次数:
227
#include "stdafx.h"#include typedef struct NODE { int data; struct NODE* pNext;}NODE,*PNODE;PNODE Create_list(void);void Traversal_list(PNODE pH...
分类:
其他好文 时间:
2014-11-26 22:10:23
阅读次数:
193
根据二叉树的前序遍历和中序遍历构造二叉树。思路:前序遍历的第一个节点就是根节点,扫描中序遍历找出根结点,根结点的左边、右边分别为左子树、右子树中序遍历。再计算左子数长度leftLength,前序遍历根结点后的leftLength长度为左子树的前序遍历,剩下的为右子树的前序遍历,代码如下: 1 /**...
分类:
其他好文 时间:
2014-11-26 13:48:33
阅读次数:
129
一个笨办法用两个Queue实现:/** * Definition for binary tree * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode(int x) { val = x; ...
分类:
其他好文 时间:
2014-11-25 17:52:34
阅读次数:
203
简单递归实现:/** * Definition for binary tree * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode(int x) { val = x; } * } */ ...
分类:
其他好文 时间:
2014-11-25 17:47:04
阅读次数:
215
给定树根root。实现中序遍历,也就是左根右。用递归的话,很简单,左边的返回值加上root的再加上右边的就行。我自己写的有点挫:/** * Definition for binary tree * struct TreeNode { * int val; * TreeNode *le...
分类:
其他好文 时间:
2014-11-25 01:34:34
阅读次数:
129
leetcode-Binary Tree Level Order Traversal 二叉树层序遍历
#include
#include
using namespace std;
typedef struct BiTree
{
int val;
struct BiTree *lchild;
struct BiTree *rchild;
}BiTree;
void main(...
分类:
其他好文 时间:
2014-11-24 22:35:13
阅读次数:
202
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
...
分类:
其他好文 时间:
2014-11-24 22:33:13
阅读次数:
272
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 ...
分类:
其他好文 时间:
2014-11-24 22:31:45
阅读次数:
196