連結:https://leetcode.com/problems/binary-tree-zigzag-level-order-traversal/description/
class Solution {
private void zigzagOrder(TreeNode root,int level,List<List<Integer>> save)
{
if (root == null)
return;
if (save.size() == level)
{
List<Integer> li = new ArrayList<Integer>();
li.add(root.val);
save.add(li);
}
else
{
if (level % 2 == 0)
save.get(level).add(root.val);
else
save.get(level).add(0, root.val);
}
zigzagOrder(root.left,level+1,save);
zigzagOrder(root.right,level+1,save);
}
public List<List<Integer>> zigzagLevelOrder(TreeNode root) {
List<List<Integer>> save = new ArrayList<List<Integer>>();
zigzagOrder(root,0,save);
return save;
}
}
連結:https://leetcode.com/problems/validate-binary-search-tree/description/
class Solution {
public boolean isValidBST(TreeNode root) {
if(root == null){
return false;
}
return isValid(root, null, null);
}
private boolean isValid(TreeNode root, Integer min, Integer max){
if(root == null){
return true;
}
if(min != null && root.val <= min){
return false;
}
if(max != null && root.val >= max){
return false;
}
return isValid(root.right, root.val, max) && isValid(root.left, min, root.val);
}
}