binary tree Archives - Coddicted

Verify Preorder Serialization of a Binary Tree 1

Title: Verify Preorder Serialization of a Binary Tree Source: leetcode.com One way to serialize a binary tree is to use pre-order traversal. When we encounter a non-null node, we record the node’s value. If it is a null node, we record using a sentinel value such as #. _9_ / \ 3 2 / \ ...

Invert Binary Tree

Title: Invert Binary Tree Source: leetcode.com Invert a binary tree. 4 / \ 2 7 / \ / \ 1 3 6 9 12345      4   /   \  2     7 / \   / \1   3 6   9 to 4 / \ 7 2 / \ / \ 9 6 3 1 12345      4   /   \  7     ...

Maximum Depth of Binary Tree

Title: Maximum Depth of Binary Tree Source: leetcode.com Given a binary tree, find its maximum depth. The maximum depth is the number of nodes along the longest path from the root node down to the farthest leaf node. Python solution Python ''' https://leetcode.com/problems/maximum-depth-of-binary-tree/ ''' # Definition for a binary tree node. # class TreeNode(object): # ...

Determine if a binary tree is height-balanced

Title: How to determine if a binary tree is height-balanced? Source: www.geeksforgeeks.org A tree where no leaf is much farther away from the root than any other leaf. Different balancing schemes allow different definitions of “much farther” and different amounts of work to keep them balanced. Consider a height-balancing scheme where following conditions should be ...

Check if a binary tree is BST

Title: A program to check if a binary tree is BST or not Source: www.geeksforgeeks.org A binary search tree (BST) is a node based binary tree data structure which has the following properties. • The left subtree of a node contains only nodes with keys less than the node’s key. • The right subtree of ...

Determine two trees are Same Tree

Title: Same Tree Source: leetcode.com Given two binary trees, write a function to check if they are equal or not. Two binary trees are considered equal if they are structurally identical and the nodes have the same value. Python solution Python ''' https://leetcode.com/problems/same-tree/ ''' # Definition for a binary tree node. # class TreeNode(object): # ...

Implementing a Generic Binary Tree in Java

Implementing a Generic Binary Tree in Java
The following two classes demonstrate an implementation of the Generic Binary Tree data structure. The first class is BinaryTreeNode.java which acts as the node class for the tree, holding integer type data and two pointers for left and right child respectively of type same as the node class itself. The member fields are declared to ...