title: Why is this code for Binary Tree failing certain test cases?
body: <p>I am working on this code challenge:</p>
  <blockquote>
  <h2>Problem Description</h2>
  <p>You are given a family tree with the ages of each person and their children in the form of a binary tree where each parent has at most 2 children. You have to check whether the family tree is valid or not i.e. the age of child nodes should be less than the parent node itself.</p>
  <h3>Input Format</h3>
  <ul>
  <li>First line contains an integer T - Number of test cases.</li>
  <li>First line of each test case contains an integer N - Number of nodes.</li>
  <li>Second line of each test case contains N integers - Age of i<sup>th</sup> person.</li>
  </ul>
  <p>Next N lines describe the binary tree in the format -</p>
  <ul>
  <li>(node, left child, right child).</li>
  </ul>
  <p>Here, node will range from 1 to N i.e. for each person there will be one line. The left child and right child will be either -1 (indicating no child) or another Node number between 1 to N indicating that it is a child.</p>
  <h3>Output Format</h3>
  <p>Print &quot;Yes&quot; or “No” depending on whether the given tree is valid or not.</p>
  <h3>Constraints</h3>
  <ul>
  <li>T &lt;= 10</li>
  <li>N &lt;= 10^5</li>
  <li>Age &lt;= 10^6</li>
  </ul>
  <h3>Sample Input 1</h3>
  <pre><code>2
  4
  10 5 9 7
  1 2 3
  2 -1 -1
  3 4 -1
  4 -1 -1
  3
  4 9 3
  1 2 3
  2 -1 -1
  3 -1 -1
  </code></pre>
  <h3>Sample Output 1</h3>
  <pre><code>Yes
  No
  </code></pre>
  <p>Note that in the given code stub, you’ll directly be given the TreeNode pointing to the head of the tree as input. You have to return true or false for that tree.</p>
  <h3>Explanation 1</h3>
  <p><a href="https://i.stack.imgur.com/68Ael.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/68Ael.png" alt="enter image description here" /></a></p>
  <p>Instructions to create custom input for a Binary Tree
  In order to specify a binary tree that can be used as custom input to the problem, you’ll need to follow this convention. (Note that this is after the &quot;Number of test cases&quot; line)</p>
  <ul>
  <li>Line 1: Number of nodes in the Binary Tree (N)</li>
  <li>Line 2: N space separated node values. The position of the Nodes on this line will be used to refer to them in the below lines, starting from 1.</li>
  <li>Line 3 to N+2: Lines specifying the child nodes for each of the N nodes</li>
  </ul>
  <p>Format of each line (space separated):</p>
  <ul>
  <li>Parent_node Left_child_node Right_child_node</li>
  <li>Parent_node - Node at this Position on Line 2 is the Node to which we are assigning the Left and Right child here</li>
  <li>Left_child_node - Node at this position on Line 2 is the left child. Specify -1 if there is no Left child.</li>
  <li>Right_child_node - Node at this position on Line 2 is the right child. Specify -1 if there is no Right child.</li>
  </ul>
  <h3>Example1</h3>
  <p><a href="https://i.stack.imgur.com/aFcNq.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/aFcNq.png" alt="enter image description here" /></a></p>
  </blockquote>
  <h2>Code</h2>
  <p>The following is my code</p>
  <pre><code>import crio.ds.Tree.*;
  /*
  // Definition of TreeNode
  public class TreeNode {
      public long val;
      public TreeNode left;
      public TreeNode right;
  
      public TreeNode (long x) {
          val = x;
          left = null;
          right = null;
      }
  }
  */
  public class Solution {
      
      static boolean validFamilyTree(TreeNode root){
  
          long left_val = -1, right_val = -1;
  
          if(root == null || (root.left == null &amp;&amp; root.right == null)){
              return true;
          }
  
          else{
              if(root.left != null){
                  left_val = root.left.val;
              }
              if(root.right != null){
                  right_val = root.right.val;
              }
              if(root.val &gt; left_val &amp;&amp; root.val &gt; right_val){
                  return true;
              } else{
                  return false;
              }
          }
      }
  }
  </code></pre>
  <p>My code succeeds for the sample input test case and some edge cases, but failing for a whole lot of other test cases and performance cases.</p>
  <p>Why does my code fail those tests?</p>