2019.7.28 #程序员笔试必备# LeetCode 从零单刷个人笔记整理(持续更新)
github:https://github.com/ChopinXBP/LeetCode-Babel
验证一棵树是不是二叉搜索树,实质上就是考察中序遍历(只有中序遍历的验证方式才是代价最少的)。
递归算法过于简单,这里就不进行实现了。循环的话主要是需要注意循环条件和操作条件。
循环法
当前结点非空时,左子树不断入栈。当前结点为空时,元素出栈,遍历其右子树。当前结点与栈均空时,遍历结束。
这里要介绍一个不借助栈进行二叉树线索化的方法(会破坏树结构):
Morris Method
Morris Method的基本原理是:不断迭代,将第一个有左子树的根节点连接在其左子树的最右结点上,最后形成一棵只有右子树的二叉树。
若左子树为空,则遍历右子树。出现第一个左子树非空的根节点,令pre成为左子树的根节点,将curr连接在pre的最右结点上。
Given a binary tree, return the inorder traversal of its nodes’ values.
Follow up: Recursive solution is trivial, could you do it iteratively?
给定一个二叉树,返回它的中序遍历。
进阶: 递归算法很简单,你可以通过迭代算法完成吗?
示例:
输入: [1,null,2,3]
1
\
2
/
3
输出: [1,3,2]
import java.util.LinkedList;
import java.util.List;
import java.util.Stack;
/**
* Given a binary tree, return the inorder traversal of its nodes' values.
* Follow up: Recursive solution is trivial, could you do it iteratively?
* 给定一个二叉树,返回它的中序 遍历。
* 进阶: 递归算法很简单,你可以通过迭代算法完成吗?
*/
public class BinaryTreeInorderTraversal {
public class TreeNode {
int val;
TreeNode left;
TreeNode right;
TreeNode(int x) {
val = x;
}
}
//循环法
public List<Integer> inorderTraversal(TreeNode root) {
List<Integer> result = new LinkedList<>();
Stack<TreeNode> stack = new Stack<>();
TreeNode p = root;
//当前结点与栈均空时,遍历结束
while(p != null || !stack.empty()){
//当前结点非空时,左子树不断入栈
while(p != null){
stack.push(p);
p = p.left;
}
//当前结点为空时,元素出栈,遍历其右子树。若右子树为空,下一次循环其父节点会出栈。
p = stack.pop();
result.add(p.val);
p = p.right;
}
return result;
}
//Morris Method:不借助栈进行二叉树线索化
public List<Integer> inorderTraversal2(TreeNode root) {
List<Integer> result = new LinkedList<>();
TreeNode curr = root;
//不断迭代,将第一个有左子树的根节点连接在其左子树的最右结点上,最后形成一棵只有右子树的二叉树
while(curr != null){
//若左子树为空,则遍历右子树
if(curr.left == null){
result.add(curr.val);
curr = curr.right;
}
//出现第一个左子树非空的根节点,令pre成为左子树的根节点,将curr连接在pre的最右结点上
else{
TreeNode pre = curr.left;
while(pre.right != null){
pre = pre.right;
}
pre.right = curr; //根节点连接在其左子树的最右结点上
TreeNode temp = curr;
curr = curr.left; //当前指针curr指回现在的根节点(曾经的pre)
temp.left = null; //曾经的根节点左子树置null
}
}
return result;
}
}
Given a binary tree, determine if it is a valid binary search tree (BST).
Assume a BST is defined as follows:
The left subtree of a node contains only nodes with keys less than the node’s key.
The right subtree of a node contains only nodes with keys greater than the node’s key.
Both the left and right subtrees must also be binary search trees.
给定一个二叉树,判断其是否是一个有效的二叉搜索树。
假设一个二叉搜索树具有如下特征:
节点的左子树只包含小于当前节点的数。
节点的右子树只包含大于当前节点的数。
所有左子树和右子树自身必须也是二叉搜索树。
示例 1:
输入:
2
/ \
1 3
输出: true
示例 2:
输入:
5
/ \
1 4
/ \
3 6
输出: false
解释: 输入为: [5,1,4,null,null,3,6]。
根节点的值为 5 ,但是其右子节点值为 4 。
/**
* Given a binary tree, determine if it is a valid binary search tree (BST).
* Assume a BST is defined as follows:
* The left subtree of a node contains only nodes with keys less than the node's key.
* The right subtree of a node contains only nodes with keys greater than the node's key.
* Both the left and right subtrees must also be binary search trees.
* 给定一个二叉树,判断其是否是一个有效的二叉搜索树。
* 假设一个二叉搜索树具有如下特征:
* 节点的左子树只包含小于当前节点的数。
* 节点的右子树只包含大于当前节点的数。
* 所有左子树和右子树自身必须也是二叉搜索树。
*/
public class ValidateBinarySearchTree {
public class TreeNode {
int val;
TreeNode left;
TreeNode right;
TreeNode(int x) {
val = x;
}
}
//Morris算法
public boolean isValidBST(TreeNode root) {
//防止出现结点值为一个或多个Integer.MIN_VALUE
double last = -Double.MAX_VALUE;
TreeNode curr = root;
while(curr != null){
if(curr.left == null){
if(curr.val <= last){
return false;
}
last = curr.val;
curr = curr.right;
}
else{
TreeNode pre = curr.left;
while(pre.right != null){
pre = pre.right;
}
pre.right = curr;
TreeNode temp = curr.left;
curr.left = null;
curr = temp;
}
}
return true;
}
//递归中序遍历
double lastNum = -Double.MAX_VALUE;
public boolean isValidBST2(TreeNode root) {
if (root == null) {
return true;
}
if (isValidBST(root.left)) {
if (lastNum < root.val) {
lastNum = root.val;
return isValidBST(root.right);
}
}
return false;
}
}
Given a binary tree, return the preorder traversal of its nodes’ values.
给定一个二叉树,返回它的 前序 遍历。
示例:
输入: [1,null,2,3]
1
\
2
/
3
输出: [1,2,3]
进阶: 递归算法很简单,你可以通过迭代算法完成吗?
import java.util.LinkedList;
import java.util.List;
import java.util.Stack;
/**
*
* Given a binary tree, return the preorder traversal of its nodes' values.
* 给定一个二叉树,返回它的 前序 遍历。
*
*/
public class BinaryTreePreorderTraversal {
public class TreeNode {
int val;
TreeNode left;
TreeNode right;
TreeNode(int x) {
val = x;
}
}
private List<Integer> result;
public List<Integer> preorderTraversal(TreeNode root) {
result = new LinkedList<>();
pre(root);
return result;
}
private void pre(TreeNode root){
if(root == null){
return;
}
result.add(root.val);
pre(root.left);
pre(root.right);
}
//非递归写法
public List<Integer> preorderTraversal2(TreeNode root){
LinkedList<Integer> result = new LinkedList<>();
if(root == null){
return result;
}
Stack<TreeNode> stack = new Stack<>();
stack.push(root);
while(!stack.isEmpty()){
TreeNode curNode = stack.pop();
//出栈后加入结果顺序:root、left、right
result.add(curNode.val);
if(curNode.right != null){
stack.push(curNode.right);
}
if(curNode.left != null){
stack.push(curNode.left);
}
}
return result;
}
}
Given a binary tree, return the postorder traversal of its nodes’ values.
给定一个二叉树,返回它的 后序 遍历。
示例:
输入: [1,null,2,3]
1
\
2
/
3
输出: [3,2,1]
进阶: 递归算法很简单,你可以通过迭代算法完成吗?
import java.util.LinkedList;
import java.util.List;
import java.util.Stack;
/**
*
* Given a binary tree, return the postorder traversal of its nodes' values.
* 给定一个二叉树,返回它的 后序 遍历。
*
*/
public class BinaryTreePostorderTraversal {
public class TreeNode {
int val;
TreeNode left;
TreeNode right;
TreeNode(int x) {
val = x;
}
}
private List<Integer> result;
public List<Integer> postorderTraversal(TreeNode root){
result = new LinkedList<>();
post(root);
return result;
}
private void post(TreeNode root){
if(root == null){
return;
}
post(root.left);
post(root.right);
result.add(root.val);
}
//非递归写法
public List<Integer> postorderTraversal2(TreeNode root){
LinkedList<Integer> result = new LinkedList<>();
if(root == null){
return result;
}
Stack<TreeNode> stack = new Stack<>();
stack.push(root);
while(!stack.isEmpty()){
TreeNode curNode = stack.pop();
//出栈后加入结果顺序:left、right、root
result.addFirst(curNode.val);
if(curNode.left != null){
stack.push(curNode.left);
}
if(curNode.right != null){
stack.push(curNode.right);
}
}
return result;
}
}
#Coding一小时,Copying一秒钟。留个言点个赞呗,谢谢你#