binary-tree

Confused - Height of Binary tree

ⅰ亾dé卋堺 提交于 2021-02-20 06:47:11
问题 I'm somewhat confused between the logic of calculating the height of binary tree. Code 1 public static int findHeight(Tree node) { if(node == null) return 0; else { return 1+Math.max(findHeight(node.left), findHeight(node.right)); } } Code 2 public static int findHeight(Tree node) { if(node == null) return -1; else { return 1+Math.max(findHeight(node.left), findHeight(node.right)); } } I think, the second one is correct, since it gives the correct answer for below code :- Tree t4 = new Tree(4

How can i get the Parent in Binary tree

可紊 提交于 2021-02-20 05:15:22
问题 How can i get the Parent in Binary tree in this code ? I wrote this : public class Node { public string state; public Node Left; public Node Right; public Node (string s , Node L , Node R ) { this.state = s; this.Right = R; this.Left = L; } public Node (string s) { this.state = s; this.Right = null; this.Left = null; } } And code this for tree of some data : 1 2 Now , how can i get the parent of Node like ( new Node("22lltrk", null, null) ) what i need to add to my code and where ? thanks .

Techniques used in checking if a binary tree is symmetric

我只是一个虾纸丫 提交于 2021-02-20 04:28:33
问题 Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center). Question link is here The recursion method need to traverse the tree twice. But one of the comment provided a solution used a technique called 'Null check'. I can't understand why in this way can we avoid checking the tree twice? Here is his code in C++: bool isSymmetric(TreeNode* root) { if (!root) return true; return isSymmetric(root->left, root->right); } bool isSymmetric(TreeNode* t1, TreeNode*

How to implement a generic in-order traversal in java binary tree?

瘦欲@ 提交于 2021-02-17 05:14:25
问题 How can I create an in-order traversal for my Binary Tree class. I have looked and tried other examples out there but cant get seem to get anything to work for me. Below is what I have so far for my in-order traversal method: public void inOrder(TreeNode<T> root) { if(root != null) { inOrder(root.left()); //Visit the node by Printing the node data System.out.printf("%d ",root.value()); inOrder(root.right()); } } public static void main(String[] args) { BinaryTree<Integer> tree = new

How to implement a generic in-order traversal in java binary tree?

白昼怎懂夜的黑 提交于 2021-02-17 05:11:55
问题 How can I create an in-order traversal for my Binary Tree class. I have looked and tried other examples out there but cant get seem to get anything to work for me. Below is what I have so far for my in-order traversal method: public void inOrder(TreeNode<T> root) { if(root != null) { inOrder(root.left()); //Visit the node by Printing the node data System.out.printf("%d ",root.value()); inOrder(root.right()); } } public static void main(String[] args) { BinaryTree<Integer> tree = new

How to implement a generic in-order traversal in java binary tree?

孤人 提交于 2021-02-17 05:11:31
问题 How can I create an in-order traversal for my Binary Tree class. I have looked and tried other examples out there but cant get seem to get anything to work for me. Below is what I have so far for my in-order traversal method: public void inOrder(TreeNode<T> root) { if(root != null) { inOrder(root.left()); //Visit the node by Printing the node data System.out.printf("%d ",root.value()); inOrder(root.right()); } } public static void main(String[] args) { BinaryTree<Integer> tree = new

Print all possible paths in binary tree

送分小仙女□ 提交于 2021-02-17 03:29:09
问题 I'm trying to print all possible paths in a binary tree. I am able to print all root to leaf paths, but cannot figure out how to add leaf-to-leaf paths.(i'm using preorder traversal for root to leaf). So, basically: If my tree is 6 / \ 4 0 / \ \ 1 3 1 If want to the code to print all the paths: 6,4,1 6,4,3 6,0,1 1,4,6,0,1 3,4,6,0,1 1,4,3 4,6,0 4,6,0,1 etc. Can anyone please help me with this binary tree? I would really appreciate your help, as I'm new to this society and to Python/Java.

How many level order BST sequences are possible given a preOrder and inOrder sequence?

放肆的年华 提交于 2021-02-11 17:44:13
问题 When I am trying to print level Order of BST, this question prompted me. Here is a Pre-Order Sequence: 4, 1, 2, 3, 5, 6, 7, 8 In_order Sequence : 1, 2, 3, 4, 5, 6, 7, 8 A level order sequence for a BST with above pre_order and In_order is [4, 2, 6, 1, 3, 5, 7, 8] However, for the same Pre-order an In-order sequence this level order sequence seems possible. [4, 1, 5, 2, 6, 3, 7, 8] . I don't know how. I am trying to figure this out. I am unable to construct BST in paper (drawing) that

Binary tree number of nodes with a given level

|▌冷眼眸甩不掉的悲伤 提交于 2021-02-11 12:42:57
问题 I need to write a program that counts the number of nodes from a certain level given in binary tree. I mean < numberofnodes(int level){} > I tried writing it without any success because I don't how to get to a certain level then go to count number of nodes. 回答1: Do it with a recursive function which only descends to a certain level. 回答2: Well, there are many ways you can do this. Best is to have a single dimensional array that keep track of the number of nodes that you add/remove at each

Are there any efficient ways to populate a balanced tree structure

ε祈祈猫儿з 提交于 2021-02-10 15:53:56
问题 I have a balanced binary tree structure: Node 0 at depth 0 is the root. The root's left child is 1 and right child is 2 , and so on. Please see image: The total depth of the tree is given as N . This N is the only parameter of the problem. Nodes at level N are designated as leaf nodes. I am storing this tree using the following node structure. struct node_s{ int n, depth, parent;//n is node number int nodescendents;//number of descendents of the current node std::vector<int> descendents;/