LeetCode 01/05/18

今天中午suki点了小米椒鸡丁,很好吃,起得一如既往地晚,起床玩了会儿就吃饭了,好吧,赶紧滚去刷题了。。

  1. Binary Tree Inorder Traversal

Inorder 用法
I will show you all how to tackle various tree questions using iterative inorder traversal. First one is the standard iterative inorder traversal using stack.

Question : Binary Tree Inorder Traversal

public List<Integer> inorderTraversal(TreeNode root) {
    List<Integer> list = new ArrayList<>();
    if(root == null) return list;
    Stack<TreeNode> stack = new Stack<>();
    while(root != null || !stack.empty()){
        while(root != null){
            stack.push(root);
            root = root.left;
        }
        root = stack.pop();
        list.add(root.val);
        root = root.right;

    }
    return list;
}

Now, we can use this structure to find the Kth smallest element in BST.

  1. Kth Smallest Element in a BST
    Question : Kth Smallest Element in a BST
 public int kthSmallest(TreeNode root, int k) {
     Stack<TreeNode> stack = new Stack<>();
     while(root != null || !stack.isEmpty()) {
         while(root != null) {
             stack.push(root);    
             root = root.left;   
         } 
         root = stack.pop();
         if(--k == 0) break;
         root = root.right;
     }
     return root.val;
 }

We can also use this structure to solve BST validation question.

  1. Validate Binary Search Tree
    Question : Validate Binary Search Tree
public boolean isValidBST(TreeNode root) {
   if (root == null) return true;
   Stack<TreeNode> stack = new Stack<>();
   TreeNode pre = null;
   while (root != null || !stack.isEmpty()) {
      while (root != null) {
         stack.push(root);
         root = root.left;
      }
      root = stack.pop();
      if(pre != null && root.val <= pre.val) return false;
      pre = root;
      root = root.right;
   }
   return true;
}
  1. Invert Binary Tree
    recursive way
TreeNode left = root.left;
        TreeNode right = root.right;
        root.left = invertTree(right);
        root.right = invertTree(left);

iterative way
use stack 不太理解

  1. Diameter of Binary Tree
    此题与110. Balanced Binary Tree相似, 两次递归
    一次求height 一次遍历整个🌲

  2. Sum of Left Leaves

if (cur.left != null) {
            if (cur.left.left == null && cur.left.right == null) {
                sum += cur.left.val;
            } else {
                sum += sumOfLeftLeaves(cur.left);
            }
        }
  sum += sumOfLeftLeaves(cur.right);
  1. Swap Nodes in Pairs
    reverse变种 定义四个node prev,cur, next, nextNext

睡前跟孔神来了个mock interview, 感觉自己被爆成渣,而且因为第一次mock, 很紧张脑子里很容易乱,好在后来在提醒下还算是写出来了

题目是扁化多层链表

public Node flatList(Node head) {
    if (head == null) {
    return null;
}
Queue<Node> que = new LinkedList<>();
Node cur = head;
if (cur.child != null) {
    cur = cur.next;
} else {
    que.offer(cur);
}
while (!que.isEmpty()) {
while (cur != null) {
        if (cur.child != null) {
        que.offer(cur);
        cur = cur.next;
}
}
cur.next = que.poll();

}
return head;
} 

public Node flatList(Node head) {
    if (head == null) {
    return null;
}
Queue<Node> que = new LinkedList<>();
Node stub = new Node();
    que.offer(head);
    while(!que.isEmpty()) {
        Node curr = que.poll();
        stub.next = curr;
        while(curr != null) {
            Stub = curr;
            if(curr.child != null) {
                que.offer(curr.child);
            }
            curr = curr.next;
        }
}
Return head;
}




Node{
    Int key;
    Node next;
Node child;
}

今天有点晚了,刷不了十道题了,早点睡了明天多写几题来弥补吧。。。

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 背景 一年多以前我在知乎上答了有关LeetCode的问题, 分享了一些自己做题目的经验。 张土汪:刷leetcod...
    土汪阅读 12,774评论 0 33
  • 目录 简书的 markdown 都不支持 [TOC] 语法……我就不贴目录了。下面按照类别,列出了29道关于二叉树...
    被称为L的男人阅读 3,379评论 0 8
  • LeetCode 刷题随手记 - 第一部分 前 256 题(非会员),仅算法题,的吐槽 https://leetc...
    蕾娜漢默阅读 17,934评论 2 36
  • 94. Binary Tree Inorder Traversal 中序 inorder:左节点->根节点->右节...
    Morphiaaa阅读 588评论 0 0
  • 1)脚下三点下压(大脚趾球骨、脚外侧骨骼、脚跟前端中心点) 2)小腿向前 3)胫骨向上 4)大腿后侧向上 5)大腿...
    皮稀稀阅读 409评论 0 0