[GeeksForGeeks] Extract Leaves of a Binary Tree in a Doubly Linked List

Given a Binary Tree, extract all leaves of it in a Doubly Linked List (DLL). Note that the DLL need to be created in-place. Assume that the node structure of DLL and Binary Tree is same, only the meaning of left and right pointers are different. In DLL, left means previous pointer and right means next pointer.

Let the following be input binary tree
        1
     /         2       3
   / \         4   5       6
 / \         / 7   8       9   10

Output:
Doubly Linked List
7<->8<->5<->9<->10

Modified Tree:
        1
     /         2       3
   /           4           6
 1 import java.util.LinkedList;
 2 import java.util.Queue;
 3
 4 public class ExtractLeavesToDLL {
 5     private TreeNode head = null;
 6     private TreeNode tail = null;
 7     public void extractLeaveNodesToDLL(TreeNode root) {
 8         recursionHelper(root);
 9     }
10     private boolean recursionHelper(TreeNode node) {
11         if(node == null) {
12             return false;
13         }
14         else if(node.left == null && node.right == null) {
15             if(head == null) {
16                 head = node;
17                 tail = node;
18             }
19             else {
20                 tail.right = node;
21                 node.left = tail;
22                 tail = node;
23             }
24             return true;
25         }
26         if(recursionHelper(node.left)) {
27             node.left = null;
28         }
29         if(recursionHelper(node.right)) {
30             node.right = null;
31         }
32         return false;
33     }
34     private void traverseDLL(TreeNode head) {
35         TreeNode curr = head;
36         while(curr != null) {
37             String prevNode = curr.left == null ? "null" : Integer.toString(curr.left.key);
38             String nextNode = curr.right == null ? "null" : Integer.toString(curr.right.key);
39             System.out.println(curr.key + "‘s prev node is " + prevNode + " and its next node is " + nextNode);
40             curr = curr.right;
41         }
42     }
43     private void traverseBT(TreeNode root) {
44         Queue<TreeNode> queue = new LinkedList<TreeNode>();
45         queue.add(root);
46
47         while(!queue.isEmpty()) {
48             TreeNode curr = queue.poll();
49             String leftNode = curr.left == null ? "null" : Integer.toString(curr.left.key);
50             String rightNode = curr.right == null ? "null" : Integer.toString(curr.right.key);
51             System.out.println(curr.key + "‘s left node is " + leftNode + " and its right node is " + rightNode);
52             if(curr.left != null) {
53                 queue.add(curr.left);
54             }
55             if(curr.right != null) {
56                 queue.add(curr.right);
57             }
58         }
59     }
60     public static void main(String[] args) {
61         TreeNode[] nodes = new TreeNode[10];
62         for(int i = 0; i < nodes.length; i++) {
63             nodes[i] = new TreeNode(i + 1);
64         }
65         nodes[0].left = nodes[1]; nodes[0].right = nodes[2];
66         nodes[1].left = nodes[3]; nodes[1].right = nodes[4];
67         nodes[2].right = nodes[5];
68         nodes[3].left = nodes[6]; nodes[3].right = nodes[7];
69         nodes[5].left = nodes[8]; nodes[5].right = nodes[9];
70         ExtractLeavesToDLL test = new ExtractLeavesToDLL();
71         test.extractLeaveNodesToDLL(nodes[0]);
72
73         //check dll is correctly constructed
74         test.traverseDLL(test.head);
75         //check binary tree is correctly modified.
76         test.traverseBT(nodes[0]);
77     }
78 }
				
时间: 2024-08-03 15:04:59

[GeeksForGeeks] Extract Leaves of a Binary Tree in a Doubly Linked List的相关文章

426.&#160;Convert Binary Search Tree to Sorted Doubly Linked List

426. Convert Binary Search Tree to Sorted Doubly Linked List https://www.youtube.com/watch?v=FsxTX7-yhOw&t=1210s https://docs.google.com/document/d/1IIn5rXrUumqpxRrMKo76FbBx1ibTBDGso5rfENmkabw/edit class Solution { public Node treeToDoublyList(Node r

[geeksforgeeks] Bottom View of a Binary Tree

http://www.geeksforgeeks.org/bottom-view-binary-tree/ Bottom View of a Binary Tree Given a Binary Tree, we need to print the bottom view from left to right. A node x is there in output if x is the bottommost node at its horizontal distance. Horizonta

[leetcode]426. Convert Binary Search Tree to Sorted Doubly Linked List二叉搜索树转有序双向链表

Convert a BST to a sorted circular doubly-linked list in-place. Think of the left and right pointers as synonymous to the previous and next pointers in a doubly-linked list. Let's take the following BST as an example, it may help you understand the p

426. Convert Binary Search Tree to Sorted Doubly Linked List - Medium

Convert a BST to a sorted circular doubly-linked list in-place. Think of the left and right pointers as synonymous to the previous and next pointers in a doubly-linked list. Let's take the following BST as an example, it may help you understand the p

LeetCode 426. Convert Binary Search Tree to Sorted Doubly Linked List

看起来很难,但是仔细想一下,实质就是二叉树的中序遍历的问题,中序遍历有递归和非递归(至少两种写法). 递归: class Solution { public: Node *prev; //实质是指向最后一个元素的指针 Node* treeToDoublyList(Node* root) { if (root==NULL) return NULL; Node *dummy=new Node(0,NULL,NULL); prev = dummy; inorder(root); prev->right

Convert a given Binary Tree to Doubly Linked List

The question and solution are from: http://www.geeksforgeeks.org/convert-given-binary-tree-doubly-linked-list-set-3/ Given a Binary Tree (BT), convert it to a Doubly Linked List(DLL) In-Place. The left and right pointers in nodes are to be used as pr

[geeksforgeeks] Convert a given Binary Tree to Doubly Linked List

http://www.geeksforgeeks.org/in-place-convert-a-given-binary-tree-to-doubly-linked-list/ Given a Binary Tree (Bt), convert it to a Doubly Linked List(DLL). The left and right pointers in nodes are to be used as previous and next pointers respectively

655. Print Binary Tree 解题报告(树)

第一部分:搜索.遍历 [例子1]655. Print Binary Tree Example 1: Input: 1 / 2 Output: [["", "1", ""], ["2", "", ""]] Example 2: Input: 1 / 2 3 4 Output: [["", "", "", "1"

[LintCode] Flatten Binary Tree to Linked List 将二叉树展开成链表

Flatten a binary tree to a fake "linked list" in pre-order traversal. Here we use the right pointer in TreeNode as the next pointer in ListNode. Notice Don't forget to mark the left child of each node to null. Or you will get Time Limit Exceeded