Correct a Binary Tree
Problem Descriptionβ
Visit LeetCode for the full problem description.
Solutionsβ
Solution 1: C# (Best: 263 ms)β
| Metric | Value |
|---|---|
| Runtime | 263 ms |
| Memory | 44.7 MB |
| Date | 2022-01-13 |
Solution
/**
* Definition for a binary tree node.
* public class TreeNode {
* public int val;
* public TreeNode left;
* public TreeNode right;
* public TreeNode(int val=0, TreeNode left=null, TreeNode right=null) {
* this.val = val;
* this.left = left;
* this.right = right;
* }
* }
*/
public class Solution {
private List<int> visited = new List<int>();
public TreeNode CorrectBinaryTree(TreeNode root) {
if(root == null) return null;
if(root.right != null && visited.Contains(root.right.val))
{
return null;
}
visited.Add(root.val);
root.right = CorrectBinaryTree(root.right);
root.left = CorrectBinaryTree(root.left);
return root;
}
}
Complexity Analysisβ
| Approach | Time | Space |
|---|---|---|
| Solution | To be analyzed | To be analyzed |