[GeeksForGeeks] Remove all half nodes of a given binary tree

Given A binary Tree, how do you remove all the half nodes (which has only one child)? Note leaves should not be touched as they have both children as NULL.

For example consider the below tree.

Nodes 7, 5 and 9 are half nodes as one of their child is Null. We need to remove all such half nodes and return the root pointer of following new tree.

Post order traversal

 1 public class RemoveHalfNodes {
 2     public static TreeNode removeHalfNodes(TreeNode root) {
 3         if(root == null) {
 4             return root;
 5         }
 6         root.left = removeHalfNodes(root.left);
 7         root.right = removeHalfNodes(root.right);
 8         if(root.left == null && root.right != null) {
 9             return root.right;
10         }
11         else if(root.left != null && root.right == null) {
12             return root.left;
13         }
14         return root;
15     }
16     private static void traverseTree(TreeNode root) {
17         if(root == null) {
18             return;
19         }
20         String leftVal = root.left == null ? "null" : String.valueOf(root.left.val);
21         String rightVal = root.right == null ? "null" : String.valueOf(root.right.val);
22         System.out.println(root.val + " left node: " + leftVal + "; right node: " + rightVal);
23         traverseTree(root.left);
24         traverseTree(root.right);
25     }
26     public static void main(String[] args) {
27         TreeNode[] nodes = new TreeNode[8];
28         for(int i = 0; i < 8; i++) {
29             nodes[i] = new TreeNode(i);
30         }
31         nodes[0].left = nodes[1]; nodes[0].right = nodes[2];
32         nodes[1].right = nodes[3];
33         nodes[2].right = nodes[4];
34         nodes[3].left = nodes[5]; nodes[3].right = nodes[6];
35         nodes[4].left = nodes[7];
36         TreeNode root = removeHalfNodes(nodes[0]);
37         traverseTree(root);
38     }
39 }
时间: 2024-10-08 10:42:10

[GeeksForGeeks] Remove all half nodes of a given binary tree的相关文章

863. All Nodes Distance K in Binary Tree 到制定节点距离为k的节点

[抄题]: We are given a binary tree (with root node root), a target node, and an integer value K. Return a list of the values of all nodes that have a distance K from the target node.  The answer can be returned in any order. Example 1: Input: root = [3

863.&#160;All Nodes Distance K in Binary Tree

https://leetcode.com/problems/all-nodes-distance-k-in-binary-tree/discuss/143752/JAVA-Graph-+-BFS https://www.youtube.com/watch?v=o1siL8eKCos class Solution { Map<TreeNode, List<TreeNode>> map = new HashMap(); //here can also use Map<TreeNo

[GeeksForGeeks] Print all nodes that don&#39;t have sibling in a binary tree.

Given a binary tree,  get all nodes that don't have sibling node, excluding the root node. Level order and pre order solutions. 1 import java.util.ArrayList; 2 import java.util.LinkedList; 3 import java.util.Queue; 4 5 public class NodeWithoutSibling

[GeeksForGeeks] Print leftmost and rightmost nodes at each level of a binary tree.

Given a Binary Tree, Print the corner nodes at each level. The node at the leftmost and the node at the rightmost. For example, output for following is 15, 10, 20, 8, 25. Solution. Level Order Traversal using queue. Core idea:  Level order traversal

[GeeksForGeeks] Connect binary tree nodes of same level

Given a binary tree with each node having one extra field of nextRight. nextRight points to the next right node of the same level. Initially all nodes' nextRight field are set to null. Connect all nodes at the same level by setting each node's nextRi

[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

[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

Print Nodes in Top View of Binary Tree

Top view of a binary tree is the set of nodes visible when the tree is viewed from the top. Given a binary tree, print the top view of it. The output nodes can be printed in any order. Expected time complexity is O(n) A node x is there in output if x

U面经Prepare: Print Binary Tree With No Two Nodes Share The Same Column

Give a binary tree, elegantly print it so that no two tree nodes share the same column. Requirement: left child should appear on the left column of root, and right child should appear on the right of root. Example: a b c d e f z g h i j 这道题若能发现inorde