Sort by

recency

|

487 Discussions

|

  • + 0 comments
    c# csharp
    
    solution that doesn't use recursion
    
    private static int getHeight(Node root)
    {
        if (root == null)
        {
            return -1; // height of empty tree is -1
        }
    
        Queue<Node> queue = new Queue<Node>();
        queue.Enqueue(root);
        int height = -1;
    
        while (queue.Count > 0)
        {
            int nodeCount = queue.Count;  // Number of nodes at the current level
            height++;
    
            // Process each node at the current level
            while (nodeCount > 0)
            {
                Node node = queue.Dequeue();
    
                // Enqueue left and right children of the current node
                if (node.left != null)
                {
                    queue.Enqueue(node.left);
                }
                if (node.right != null)
                {
                    queue.Enqueue(node.right);
                }
    
                nodeCount--;
            }
        }
    
        return height;
    }
    
  • + 0 comments

    This is what I came up with:

        def getHeight(self,root):
            if root is None:
                return -1
            else:
                return max(self.getHeight(root.left),self.getHeight(root.right)) + 1
    
  • + 0 comments

    Using Python 3’s pattern-matching:

    def getHeight(self, root):
        match root:
            case Node(left=None, right=None):
                return 0
            case Node(left=None):
                return 1 + self.getHeight(root.right)
            case Node(right=None):
                return 1 + self.getHeight(root.left)
            case _:
                return max(1 + self.getHeight(root.left), 1 + self.getHeight(root.right))
    
  • + 0 comments

    Python 3:

    def getHeight(self, root) -> int:
    		#Write your code here        
    		if root.left:
    				left = self.getHeight(root.left) + 1
    		else:
    				left = 0
    
    		if root.right:
    				right = self.getHeight(root.right) + 1
    		else:
    				right = 0
    
    		return max(left, right)
    
  • + 0 comments

    in TS

    class TreeNode {
        data: number
        left: TreeNode | null = null
        right: TreeNode | null = null
        
        constructor(n: number) {
            this.data = n
        }
    }
    
    class BinarySearchTree {
        root: TreeNode | null = null
    
        insert(root: TreeNode | null, data: number): void {
            if (root === null) {
                const newNode = new TreeNode(data);
                if (this.root === null) {
                    this.root = newNode;
                } else {
                    root = newNode;
                }
                return;
            }
    
            if (data <= root.data) {
                if (root.left) {
                    this.insert(root.left, data);
                } else {
                    root.left = new TreeNode(data);
                }
            } else {
                if (root.right) {
                    this.insert(root.right, data);
                } else {
                    root.right = new TreeNode(data);
                }
            }
        }
    
        getHeight(root: TreeNode | null): number {
            if (root === null) {
                return 0;
            }
    
            const leftHeight = this.getHeight(root.left);
            const rightHeight = this.getHeight(root.right);
            return Math.max(leftHeight, rightHeight) + 1;
        }      
    
    }