[leetcode] 题型整理之二叉树

94. Binary Tree Inorder Traversal

Given a binary tree, return the inorder traversal of its nodes' values.

For example:
Given binary tree [1,null,2,3],

   1
\
2
/
3

return [1,3,2].

Note: Recursive solution is trivial, could you do it iteratively?

非递归前序遍历:

/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
public class Solution {
public List<Integer> inorderTraversal(TreeNode root) {
List<Integer> result = new ArrayList<Integer>();
Stack<TreeNode> stack = new Stack<TreeNode>();
TreeNode hand = root;
while (hand != null) {
stack.push(hand);
hand = hand.left;
}
while (!stack.isEmpty()) {
TreeNode node = stack.pop();
result.add(node.val);
hand = node.right;
while (hand != null) {
stack.push(hand);
hand = hand.left;
}
}
return result;
}
}

103. Binary Tree Zigzag Level Order Traversal

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,null,null,15,7],

    3
/ \
9 20
/ \
15 7

return its zigzag level order traversal as:

[
[3],
[20,9],
[15,7]
]
这其实是层次遍历,但是每一层之后都要在队列中加入null。当从队列中读到null的时候,说明旧的一层结束了。
root之后的null在循环前加入。
读到最后一层之后的null的时候要特殊处理,要判断是否是空队列,是的话就不再加入null。
109. Convert Sorted List to Binary Search Tree
Given a singly linked list where elements are sorted in ascending order, convert it to a height balanced BST.
关键要有一个currentNode, 作为域被各个函数调用并不断更新。
length==2和3也要当作特殊例子处理,否则会超时。

左子树根据长度创建完以后除了给出左子树的根节点还可以直接给出根节点(左子树的父节点)。

创建左子树的时候要不停地更新currentNode。

/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) { val = x; }
* }
*/
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
public class Solution {
ListNode currentNode;
public TreeNode sortedListToBST(ListNode head) {
int length = 0;
ListNode hand = head;
while (hand != null) {
length++;
hand = hand.next;
}
currentNode = head;
return helper(length);
}
private TreeNode helper(int length) {
if (length == 0) {
return null;
}
if (length == 1) {
TreeNode result = new TreeNode(currentNode.val);
currentNode = currentNode.next;
return result;
}
if (length == 2) {
TreeNode l = new TreeNode(currentNode.val);
currentNode = currentNode.next;
TreeNode result = new TreeNode(currentNode.val);
currentNode = currentNode.next;
result.left = l;
return result;
}
if (length == 3) {
TreeNode l = new TreeNode(currentNode.val);
currentNode = currentNode.next;
TreeNode result = new TreeNode(currentNode.val);
currentNode = currentNode.next;
result.left = l;
TreeNode r = new TreeNode(currentNode.val);
currentNode = currentNode.next;
result.right = r;
return result;
}
TreeNode left = helper(length / 2);
TreeNode root = new TreeNode (currentNode.val);
currentNode = currentNode.next;
root.left = left;
root.right = helper(length - length / 2 - 1);
return root;
} }

148. Sort List

Sort a linked list in O(n log n) time using constant space complexity.

用mergesort做,跟上一题超级像。

144. Binary Tree Preorder Traversal

Given a binary tree, return the preorder traversal of its nodes' values.

For example:
Given binary tree {1,#,2,3},

   1
\
2
/
3

return [1,2,3].

Note: Recursive solution is trivial, could you do it iteratively?

非递归前序遍历

用栈,先根节点入栈之后一个个pop出来,父节点打印,右节点入栈,左节点作为先一个hand,直到hand是null为止。

/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
public class Solution {
public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) {
if (root == null) {
return null;
}
if (p == root || q == root) {
return root;
}
TreeNode left = lowestCommonAncestor(root.left, p, q);
TreeNode right = lowestCommonAncestor(root.right, p, q);
if (left == null) {
return right;
} else if (right == null) {
return left;
} else {
return root;
}
}
}

255. Verify Preorder Sequence in Binary Search Tree

Given an array of numbers, verify whether it is the correct preorder traversal sequence of a binary search tree.

You may assume each number in the sequence is unique.

Follow up:
Could you do it using only constant space complexity?

解释参见grandyang的博客

[LeetCode] Verify Preorder Sequence in Binary Search Tree 验证二叉搜索树的先序序列

java代码如下:

public class Solution {
public boolean verifyPreorder(int[] preorder) {
Stack<Integer> stack = new Stack<Integer>();
int length = preorder.length;
if (length == 0) {
return true;
}
int low = Integer.MIN_VALUE;
int count = 0;
for (int i = 0; i < length; i++) {
int num = preorder[i];
if (num < low) {
return false;
}
while (count > 0 && preorder[count - 1] < num) {
count--;
low = preorder[count];
}
preorder[count] = num;
count++;
}
return true;
}
}
上一篇:python学习笔记十二:类的定义


下一篇:python学习之Numpy.genfromtxt