linked list焦点问题,面经里很多,考虑相交不相交,有环无环 + Find Leaves of Binary Tree (Java)

break the loop at the last node which pointed to the entry.

Given a binary tree, collect a tree‘s nodes as if you were doing this: Collect and remove all leaves, repeat until the tree is empty.

Example:
Given binary tree

          1
         /         2   3
       / \
      4   5

Returns [4, 5, 3], [2], [1].

ollowup: 每个node还是有left,right指针,但是结构并非tree,而是graph怎么算, 考虑有/无circular dependency 两种情况;

方法是用hashmap 记录计算过的node level 和 一个Set 记录dependent node

public List<List<Integer>> findLeaves(TreeNode root) {
    List<List<Integer>> result = new ArrayList<List<Integer>>();
    helper(result, root);
    return result;
}

// traverse the tree bottom-up recursively
private int helper(List<List<Integer>> list, TreeNode root){
    if(root==null)
        return -1;

    int left = helper(list, root.left);
    int right = helper(list, root.right);
    int curr = Math.max(left, right)+1;

    // the first time this code is reached is when curr==0,
    //since the tree is bottom-up processed.
    if(list.size()<=curr){
        list.add(new ArrayList<Integer>());
    }

    list.get(curr).add(root.val);

    return curr;
}

  

时间: 2024-11-19 15:54:23

linked list焦点问题,面经里很多,考虑相交不相交,有环无环 + Find Leaves of Binary Tree (Java)的相关文章

刷题114. Flatten Binary Tree to Linked List

一.题目说明 题目114. Flatten Binary Tree to Linked List,将一个二叉树"原地"压缩为"链表"形态的二叉树.难度为Medium! 二.我的解答 这个题目如果允许使用栈的话Easy,先序遍历二叉树,右子树入栈,左子树入栈.当栈不空的时候,将栈顶元素放到右子树即可. class Solution{ public: void flatten(TreeNode* root){ //先根遍历 if(root==NULL) return;

判断链表是否有环及环入口点的求法(Linked List Cycle II )

分为两步 第一步 还是利用快慢指针,如果有环的话在利用快慢指针终会相会于一个节点. 第二步.然后从这节点出发每次出发走一步,同时从根节点出发每次出发也走一步则他们两个指针相遇的地方就是环的入口. 第一步好解释那么第二步是为什么呢? 网上有很多解法大都是从数学的角度来分析,有公式也有推算很不直观,我从图形的角度来看的话就相对理解起来简单很多. 将图摊开成一条线,假设我们有环而且假设快指针就多走了一圈就与慢指针相遇了(多走n圈其实也是一样的,画出图来也不难理解,只是画起来麻烦索性就以一圈来代表) 红

【leetcode】Flatten Binary Tree to Linked List (middle)

Given a binary tree, flatten it to a linked list in-place. For example,Given 1 / 2 5 / \ 3 4 6 The flattened tree should look like: 1 2 3 4 5 6 思路: 用先序遍历,得到的是从小到大的顺序.把先序遍历变形一下: void flatten(TreeNode* root) { if(NULL == root) return; vector<TreeNode *

Flatten Binary Tree to Linked List

Given a binary tree, flatten it to a linked list in-place. For example, Given 1 / 2 5 / \ 3 4 6 The flattened tree should look like: 1 2 3 4 5 6 class Solution { public: TreeNode *node = NULL; void flatten(TreeNode *root) { if(root == NULL) return; i

!!!!!!114. Flatten Binary Tree to Linked List

Given a binary tree, flatten it to a linked list in-place. For example,Given 1 / 2 5 / \ 3 4 6 The flattened tree should look like: 1 2 3 4 5 6 ======== 思路: 将一个二叉树 就地 压成"链表"的结构;

LeetCode OJ:Flatten Binary Tree to Linked List(捋平二叉树)

Given a binary tree, flatten it to a linked list in-place. For example,Given 1 / 2 5 / \ 3 4 6 The flattened tree should look like: 1 2 3 4 5 6 也就是说用先序遍历的方式将所有的节点都放到右侧来,我这里的方法的主要思想就是每次左侧存在节点的时候就将其原封不动的搬移到右侧来,代码如下: 1 /** 2 * Definition for a binary tr

leetCode(42):Flatten Binary Tree to Linked List

Given a binary tree, flatten it to a linked list in-place. For example, Given 1 / 2 5 / \ 3 4 6 The flattened tree should look like: 1 2 3 4 5 6 If you notice carefully in the flattened tree, each node's right child points to the next node of a pre-o

Java for LeetCode 114 Flatten Binary Tree to Linked List

Given a binary tree, flatten it to a linked list in-place. For example, Given 1 / 2 5 / \ 3 4 6 The flattened tree should look like: 1 2 3 4 5 6解题思路:试图通过排序后new TreeNode是无法通过的,这道题的意思是把现有的树进行剪枝操作.JAVA实现如下: static public void flatten(TreeNode root) { wh

LeetCode (15) Flatten Binary Tree to Linked List

题目描述 Given a binary tree, flatten it to a linked list in-place. For example, Given The flattened tree should look like: 本题也是考察二叉树和指针操作的题目.题目要求将一棵二叉树拉平为一个链表 .链表通过树节点的右子树相连,且展开的顺序为原来树的前序遍历. 实现思路: 若节点n存在左子树left,则将左子树连接为n的右子树 若n存在右子树(n->right存在),则将n->ri